I have two versions of my model Model001.xcdatamodel
and Model002.xcdatamodel
. These two are in the Model.xcdatamodeld
bundle.
I also have a Model001to002.xcmappingmodel
which is not part of the Model.xcdatamodeld
. I checked: both the xcmappingmodel and the xcdatamodeld get copied into the .app bundle.
My managed object context is initialized like this:
NSURL *documentModel = [bundle URLForResource:@"Model" withExtension:@"momd"]; managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:documentModel]; return managedObjectModel;
I also set these properties on my overridden initWithFileURL:
in my UIManagedObject
subclass.
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithDictionary:self.persistentStoreOptions]; [options setObject:@YES forKey:NSMigratePersistentStoresAutomaticallyOption]; [options setObject:@YES forKey:NSInferMappingModelAutomaticallyOption]; self.persistentStoreOptions = [options copy];
But when I try to open a documet, I get the following error:
Can't find mapping model for migration
-- UPDATE --
Even if I do a manual migration
[NSMappingModel mappingModelFromBundles:@[[NSBundle mainBundle]] forSourceModel:sourceObjectModel destinationModel:self.managedObjectModel];
this returns nil. Although I double checked that the Model001to002.cdm is in the app bundle. It has to be in the app bundle right?
Usually it seems you need to restart Xcode. If that doesn't do it you might try re-selecting the destination at the bottom of the model editor.
Migrations happen in three steps: First, Core Data copies over all the objects from one data store to the next. Next, Core Data connects and relates all the objects according to the relationship mapping. Finally, enforce any data validations in the destination model.
OK, solved the problem by removing all core data files from Xcode, reading them and setting the source and destination of the mapping model again.
Damn you Xcode!
A "gotcha" with mapping models is that you are not allowed to make any changes to the models after you created the mapping. If you do, you will also get this error.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With