2
votes

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). 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?

1
I'm sure it's a problem with the migration; launching with a fresh core data works fine.johnnyspo
Couldn't figure it out, so I started over. Thought I did the same things as before, but this time it works. What I did: i) created an inverse relationship A <-->> B, ii) auto-generated the B implementation file, iii) map from v2 to v3, did not specify any value expression for the relationship, nor did I specify a migration policy from v2 to v3 (yes for v1 to v3), iv) manually added @property in A for relationship with B. Hope this helps someone else.johnnyspo

1 Answers

2
votes

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