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?
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With