Search code examples
objective-cmacoscore-datanspersistentdocument

NSPersistentDocument crash when creating persistent store


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!


Solution

  • 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.