Loading CoreData model fails in simulator

Not finding much to match this error message from Google, I am posting this:

I had cause to generate a new version of a CoreData model (xcdatamodel -> xcdatamodeld) and although I was able to happy rely on the automatic model migration when on device, it repeatedly crashed on simulator. The message was:

2010-08-27 16:44:12.566 WildObsLookout[47240:207] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Bad model.  For entity 'WildObsSearchHistory' superentity 'Account' does not think I am a subentity'

Crazy thing was … I didn’t have any such entity, all mine were prefixed with WildObs.

Turns out this code (not sure where I grabbed it from) was being over zealous on simulator:

NSMutableSet *allBundles = [[[NSMutableSet alloc] init] autorelease];
[allBundles addObjectsFromArray: [NSBundle allBundles]];
[allBundles addObjectsFromArray: [NSBundle allFrameworks]];
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:[allBundles allObjects]] retain];

The fix was to only load the models I wanted and hand merge them:

NSString *modelPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"WildObs.momd"];
NSManagedObjectModel *model = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]];

There is no doubt a better way to load only my *.mom files w/o picking each one individually, but I’ll look for that when I get to version #3. I hope this helps somebody.

Posted in Programming Tagged with: