Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using mergedModelFromBundles: and versioning (CoreData)

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?

like image 234
charlax Avatar asked Feb 17 '10 15:02

charlax


People also ask

When should I use CoreData?

Use Core Data to save your application's permanent data for offline use, to cache temporary data, and to add undo functionality to your app on a single device. To sync data across multiple devices in a single iCloud account, Core Data automatically mirrors your schema to a CloudKit container.

What is iOS CoreData?

Core Data is an object graph and persistence framework provided by Apple in the macOS and iOS operating systems. It was introduced in Mac OS X 10.4 Tiger and iOS with iPhone SDK 3.0. It allows data organized by the relational entity–attribute model to be serialized into XML, binary, or SQLite stores.

Is CoreData a framework?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life cycle and object graph management, including persistence.


4 Answers

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.

like image 132
Marcus S. Zarra Avatar answered Oct 17 '22 15:10

Marcus S. Zarra


Also, if you rename your models at any point, be sure and repeat the "set current model" step by switching to an older then back to the newer model again. My build setting would not reset itself automatically and kept setting the "current model name" to a nonexistent model, resulting in the exact same problem.

You can always verify this setting is correct in the build product's resource folder, inside the imported .momd directory, in a file called versioninfo.plist - the setting for the current model MUST match the actual name of your model.

like image 9
SG. Avatar answered Oct 17 '22 15:10

SG.


I've found that using the mergedModelFromBundel: method doesn't seem to work with migration; I switched to -initWithContentsOfURL:, and it works fine. Note that you have to init it with a URL pointing to a ".momd" file.

like image 4
Ben Gottlieb Avatar answered Oct 17 '22 17:10

Ben Gottlieb


If you change your data model at any point, be sure to Reset Content and Settings in the simulator, or you will get the error that the version of the model used to make the store is not the same as the model used to access it.

like image 2
PapaSmurf Avatar answered Oct 17 '22 15:10

PapaSmurf