Core data migration involving a new entity and a to-many relationship
I am attempting to add a new entity (say B, with two "double" attributes) to my core data model and create an optional to-many relationship with an existing entity (say A)开发者_StackOverflow中文版. I created the new model revision that includes the new entity B, the existing entity A, and a to-many relationship A ->> B. This new model is the default model. I created a mapping model from v2 to v3 (v1 -> v2 migration works fine, just data type changes for entity A), but did not specify a value expression for A's relationship to B, nor value expressions for B's attributes.
When I run the app, I receive the following error when attempting to instantiate the managed object model managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0'.
The problem is probably that I did not specify a value expression for A's relationship to B in the mapping model, but I thought that its not necessary because B is optional and it did not exist in v2 of the model. If I do have to specify a value expression for relationship, how do I do so for a non-existent (as it doesn't exist in v2 of the model) optional relationship?
Any help is greatly appreciated.
PS - one more question - is a mapping model even necessary, or is automatic migration smart enought to to handle the new entity and the relationship?
I just resolved this problem in my own project. [NSManagedObjectModel mergedModelFromBundles:nil] is not playing nice when you've multiple versions of your datamodel. It's trying to include all of them, and it shouldn't.
Try using something like this instead:
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel != nil) {
return managedObjectModel;
}
NSString *path = [[NSBundle mainBundle] pathForResource:@"Foo" ofType:@"momd"];
NSURL *momURL = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];
return managedObjectModel;
}
For the in depth story check: http://iphonedevelopment.blogspot.com/2009/09/core-data-migration-problems.html
精彩评论