views:

4943

answers:

5

I have read in the documentation about automatic /lightweight migration for Core Data models - but am having problems in the reality of implementing it.

As I understand it the application should notice that the model it has and the model that exists on a device already are not the same. If you have only added attributes or relationships and similar simple changes then the model should be upgraded automatically.

Any pointers - do I need to set something in xCode?

+20  A: 

I've now found out that this is quite simple - once you know where to look.

In my AppDelegate I set-up the NSPersistentStoreCoordinator - and you need to add some options to this to tell it to handle auto-migrate:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:

[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,

[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

NSError *error;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];

if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
 // Handle error
 NSLog(@"Problem with PersistentStoreCoordinator: %@",error);
}

Then you need to do a little trick in xCode:

  1. Select your xcdatamodel file
  2. Select the Design Menu at the top - then Data Model - then choose Add Model Version
  3. Your xcdatamodel file will then get moved into a new directory with the same name as your xcdatamodel file but with the extension xcdatamodeld - there will be a second file in this directory with a 2 in the name. Select the new file and then Design->Data Model->Set Current Version
  4. If you have already made the changes that have caused your project to be incompatible - take these changes out of the original xcdatamodel file. If you have yet to make the changes - then just edit the 2.xcdatamodel file (the one you just made current version).
  5. Now when you install this version onto a device that has the old model - it will automatically upgrade that model to the new model.

This seems great and as simple as I wanted - but I think you need to be careful during development as you change a model - otherwise you will have to create a new version for each change.

I think what I will do is that I will keep all of the changed files and then once I get ready to deploy my update I'll delete all the in-between files and just deploy with the oldest and latest models.

Grouchal
I'd suggesting reading the Core Data Migration and Versioning Guide, available in the Xcode doc viewer and on developer.apple.com.
Hunter
I didn't find the answer in there laid out simply - it took a while to find out how to do this.
Grouchal
NSInferMappingModelAutomaticallyOption works well, but only for simple mappings, like changing the name of an attribute. For anything more complicated (relationships, removing or adding entities) you will need to add a mapping model. If Xcode complains that NSInferMappingModelAutomaticallyOption is undeclared, add #import <CoreData/CoreData.h> to your app delegate header file.
Elise van Looij
I added some entities to my model and got the infamous "The model used to open the store is incompatible with the one used to create the store" error. Your solution fixed it! Thank you very much!
Karsten Silz
Thank you very much for this explanation ! It works perfectly !
Christophe Konig
Thanks for this post. I'd been reading up on this all day and there was nothing online that directed me to the Data Modelling menus. This worked a treat, and my faith in Core Data has been renewed :)
Steve Neal
+7  A: 

This was incredibly helpful. The Apple documentation was -- as usual -- woefully incomplete. I recommend doing a clean build, as I ran into an error "Can't merge models with two different entities xxx" when I first ran after making these changes. The clean build fixed it up.

A clean build fixed my issues, too.
jrainbow
+1  A: 

You are lucky not to be around, because I would I tried to kiss you :))) Thanks a lot, this was very helpful indeed.

Fran
A: 

Grouchal's answer is perfect...but if you are still having the "Can't merge models with two different entities xxx" even after cleaning up the build several times...Your might have issues with how the managedObjectModel is being loaded...take at look at this one...which helped me fix it..

http://iphonedevelopment.blogspot.com/2009/09/core-data-migration-problems.html

Santthosh
A: 

i followed all these steps but still getting errors can anyone help me???

Sneha Patel
Yes I can help, I have magically thought the answer to you in the same way you magically thought the error description to me and all the other details that might help me to help you! If you find that my magical thought solution doesn't work for you - then you might want to try the more conventional idea of writing down what the actual problem that you are having is and stop believing in magic!
Grouchal