0

I am trying to update an application that currently uses straight SQLite to use Core Data. In the current database, there are several many-to-many relationships that I will have to migrate over. This was setup by creating an index/id column in the "entity" tables, and using those id's to reference each row.

Since Core Data handles the indexing and relationships between entities, how do I rebuild these relationships when I am copying the data over? Do I have to add an ID attribute so I can maintain the original references? Is there a better way?

Also, is this something I can use a migration manager for?

Thanks!

2
  • To me core data seems not efficient to handle many to many relationship. Commented Jan 16, 2014 at 15:04
  • Core Data handles many-to-many just fine; it does it the exact same way you would in a straight database situation. Commented Jan 16, 2014 at 17:51

1 Answer 1

1

Let’s imagine you’re doing this for the database where there is many-to-many relationships between entities Photo and Tag. And you created the same Core Data model.

So you’re iterating over the photos from the old database. You get the next photo and all the tags for it, and you have it all in some intermediate format. Then you create Photo managed object and Tag managed objects for each of the tags. Your Photo object will have tags property representing to-many relationship with Tag entity. Create an NSSet from the tags you created any simply call [photo addTags:tags];. Core Data will take care about the reverse relationship from each of those tags to the photo.

Unfortunately, you can’t use the migration manager for that. It’s for doing manual migration between Core Data models.

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect. I would add that it helps to envision this as an import to Core Data rather than a migration.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.