Search code examples
iphoneiosipadcore-datadata-migration

Core Data Migration - Is this a simple migration, how should it be done?


What I'm Doing:

I'm pretty new to Core Data, and I've realized that my current use of it in my app is far from good. Basically, I've got an entity with about four different string properties. Every time I work with the database, I just pass around arrays with each of the four strings.

Now, what I want to do is add more properties to this entity. It would also be great if I could add methods to this entity to save a lot of repetition whenever I use the database in different classes.

So, I've figured that the best way (and probably how I should have done it originally) is to use Xcode to create a class for this entity - there's plenty of tutorials online for this. That way, I could just pass instances of this class around - much cleaner than using arrays!


Where I'm stuck:

Is there an easy way to migrate from my current core data model to one with added properties and a corresponding class? I also want to split one of the existing strings into two as the migration happens (name into first + last name).

Should I be using the built in migration tools? Or, should I just create a new database model completely from scratch, and build a custom class that checks for the old database, converts each object and stores it in the newer? What would be the best way to go about this - as I'm new to databases/core data, I don't want to start just to find out there's an easier or better way to do it.


Solution

  • You need to create a managed object model bundle, and to enable migration. This essentially allows you to load more than one database model in order to retrieve the same type of data. Then you can tell the managed object context to copy everything over. For more info: http://www.timisted.net/blog/archive/core-data-migration/.