Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't find mapping model for migration - UIManagedDocument Core Data Migration

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?

like image 273
V1ru8 Avatar asked Oct 08 '12 13:10

V1ru8


People also ask

Can t find mapping model for migration Xcode?

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.

How do I migrate to core data?

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.


2 Answers

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!

like image 22
V1ru8 Avatar answered Oct 12 '22 23:10

V1ru8


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.

like image 140
Albin Stigo Avatar answered Oct 13 '22 00:10

Albin Stigo