0

Recently I added a new entity into my Core Data model, so I created a new version for the model and a mapping model for it. However, now my NSPersistentDocument crashes with no obvious reason:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
                                                                        [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

BOOL success = [self configurePersistentStoreCoordinatorForURL:storeURL ofType:typeName modelConfiguration:nil storeOptions:options error:error]; // Line that crashes

The console logs:

*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil

Here is the stack trace if it helps: Stacktrace

Removing the mapping model doesn't help, so I guess its because the document tries to load the wrong/none data model but I haven't found a way to say that it should use a given data model.

Edit: When I use my own Core Data abstraction class for iOS, everything is fine. So the root of all evil seems to be NSPersistentDocument. Actually I don't want to switch back to NSDocument and have to implement the Core Data handling myself again, so any help is really appreciated!

JustSid
  • 25,168
  • 7
  • 79
  • 97

2 Answers2

0

You've probably made a change to the model that the automatic or "inferred" migration cannot handle. You will probably have to supply a mapping model to detail how the migration should be done.

Oh, and make sure that your old and new models are versioned i.e. has a version number. If not, the automatic migration can't tell which model is the old one and which the new.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • The problem is, I have a mapping model. And both data models are in and .xcdatamodeld container and the new one has "Current version" set. – JustSid Mar 09 '11 at 16:10
  • Okay, after some more testing and playing around I found the real reason, the old .mom file was still there plus the new .momd file. So basically you were right with your answer. – JustSid Mar 10 '11 at 12:28
  • Oh, yeah, I forgot about that. For some reason, the old compiled mom files can hang around after you've added a new one. You usually have to delete the app off the simulator/device and clean the build to get rid of it. Hopefully, they fixed that in Xcode 4. – TechZen Mar 10 '11 at 15:05
0

Okay, I got it working by overwriting - (id)managedObjectModel and returning a valid managed object model myself. Looks like NSPersistentDocument isn't able to do this by itself for models with multiple versions.

JustSid
  • 25,168
  • 7
  • 79
  • 97
  • It could have been a corrupted model file. They are just XML/plist after all. It's rare but they do get mangled sometimes. – TechZen Mar 09 '11 at 21:13