Using mergedModelFromBundles: and versioning (CoreData)

charlax picture charlax · Feb 17, 2010 · Viewed 16k times · Source

I'm trying to use the migration feature in CoreData. I've followed the Apple Documentation. I have a problem in the following method:

/**
 Returns the managed object model for the application.
 If the model doesn't already exist, it is created by merging all of the models found in the application bundle.
 */
- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    /* 
     * NSInvalidArgumentException', reason: '*** -[NSCFArray insertObject:atIndex:]: attempt to insert nil'
     * 2010-02-17 16:27:15.338 Patrimoine[3037:207]
     */ 
    managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    
    return managedObjectModel;
}

It appears that there is the same problem on http://iphonedevelopment.blogspot.com/2009/09/core-data-migration-problems.html

Yet I did choose the method Apple suggests, by using the menu option "Add Model Version".

Do you have any idea?

Answer

Marcus S. Zarra picture Marcus S. Zarra · Feb 17, 2010

You need to clean your project. As soon as you "version" your model Xcode moves it into a bundle (folder) but it does not remove the old one. What happens then is that the next time you run your app there are two copies of your model in the bundle; the old one and the new one that is inside of the momd bundle.

Doing a Project -> Clean All will resolve this issue.