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?
Unfortunately for me, it wasn't an issue of cleaning or versioning. I have a somewhat complicated project with several layers of nested Xcode projects. To handle the issue of each project being in different SVN repository, I set up the paths to my projects as relative to some specific source trees that I set up in the Xcode "Preferences --> Locations". For instance, I set up FACEBOOK_IOS_SDK_SRC to point to "~/Documents/{my_crazy_folder_structure}/facebook-ios-sdk/src". This way, whenever my colleagues checkout my project, the location of the Facebook SDK is relative to whatever path they set up and doesn't have to be in the exact same structure on their machines as it is on mine. I know there are better ways to handle this but it is working for now.
Anyways - my issue came up because at some point I had my *.xcdatamodel file location as "Relative to {source_tree_path}", which found the file just fine, and even compiled it into a *.mom file but I was getting this crazy error listed in the initial question. I changed my location to "Relative to Group" and it started working. I haven't found the exact reason for this yet but it wasted the better part of a day investigating. Hopefully this will help someone else out in the future.
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.
At first, none of the mentioned methods worked for me. I'm using XCode 4. Performed Clean, Clean Build Folder, located the .app file to check there's no other .xcdatamodel file there except the correct one (containing both old and new versions). Tried to use initWithContentsOfURL:. Verified the current version corresponds to the correct one and did Reset Content and Settings on the simulator. Then edited the VersionInfo.plist and discovered that the NSManagedObjectModel_CurrentVersionName value corresponds to a non-existing version name!
So reset the current version name for some existing model (you then see the green checkmark changing), rebuilt and it worked.
FYI!
I started getting crashes for
[[NSManagedObjectModel mergedModelFromBundles:nil]
as well, with no error message (typical core data). I eventually solved it by realising that I had named two entities in different models with the same name. That is why it was crashing for me. Changing the names stopped it crashing.Thanks to everyone!
These all worked, but only after I used the Clean build option.
See this other Stack Overflow topic for more info: Using mergedModelFromBundles: and versioning (CoreData)
The iPhone Core Data Reference has a fairly decent write up on this. The formal process from the documentation is an "Automatic Lightweight Migration".
Thanks again, --Batgar
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.