1

I'm trying to upgrade my current application to use an abstract parent entity, with specialized sub entities. I've created a custom NSEntityMigrationPolicy, and in the mapping model I've set the Custom Policy to the name of my class.

I'm initializing my persistent store like this, which should be fairly standard :

NSError *error=nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                    [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
                     nil];

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
    NSLog(@"Error adding persistent store : %@",[error description]);
    NSAssert(error==nil,[error localizedDescription]);
}    

When i run the app i get the following error :

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'The operation couldn’t be completed. (Cocoa error 134140.)'

[error userInfo] contains "reason=Can't find mapping model for migration"

I've verified that version 1 of the data model will open, and if i set NSInferMappingModelAutomaticallyOption i get a migration, although my entities are not migrated correctly (as expected).

I've verified that the mapping model (cdm) is in the application bundle, but somehow it refuses to find it. I've also set breakpoints and NSLog() statements in the custom migration policy, and none of it runs, with or without NSInferMappingModelAutomaticallyOption

Any hints as to why it seems unable to find the mapping model ?

westsider
  • 4,967
  • 5
  • 36
  • 51

2 Answers2

2

First, abstract entities have a rather large penalty attached to them. All children of the abstract will be stored in the same table. This will create a very wide table with a lot of voids. I would suggest reviewing your data model and making sure that is what you really want.

Second, if you cannot find the mapping model that means it is not lining up with either the source or the destination. If you changed the destination after creating the mapping model, it won't find the map. Creating a mapping model needs to be the last step after the new model is finished. I would even recommend locking the model.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • I've come to the same conclusion after examining the database produced by my app on the simulator. If all entities end up in the same table anyway, i can surely design my schema in a more optimized way, a way that may even allow for a simple migration. – Jimmy Selgen Nielsen Mar 09 '11 at 15:37
  • While my question still isn't answered (i've created the migration after my schema, several times), i'm marking it as resolved. Maybe abstract parent entities is a bit overkill for an iOS application. – Jimmy Selgen Nielsen Mar 09 '11 at 15:41
  • After days of pulling my hair out, this helped me. I deleted my mapping policy and the mapping model and started over. Marcus is right, creating the mapping model and the custom policy should be the last thing you do. Many thanks. – RyanM Jun 08 '12 at 18:08
0

I seem to have resolved the above issue, although I have no clue what i did.

Having tried for 4 days to get the migration working, I finally gave up, scratched my design, and started over. Somewhere in the process I reset git's master branch to an earlier branch to check up on some details, and I accidentally fired up the debugger. Much to my surprise, it now ran the migration just fine, and everything worked.

My best guess is that XCode had some old migrations that it failed to delete while cleaning the build (even though i've manually deleted the build directory)