I’m receiving an 'Cannot create an NSPersistentStoreCoordinator with a nil model' error after deleting my application from device. I’m testing an iPhone app in an iPad device. I’ve put this code to check if I have the file in AppDelegate.m:
- (NSManagedObjectModel *)managedObjectModel {
if (__managedObjectModel != nil) {
return __managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Name" withExtension:@"momd"];
if ([[NSFileManager defaultManager] fileExistsAtPath:[modelURL path]]) {
NSLog(@"%@", [modelURL path]); //This is printed because file exists
}
__managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return __managedObjectModel;
}
The problem is that [NSManagedObjectModel initWithContentsOfURL] is returning nil value.
I’ve done the following things, with no success:
- Change managedObjectModel instantiation with this
__managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil]; - Cleaned Build Folder and Cleaned project
- Restarted Xcode
- Restarted computer
- Changed “momd” to “mom”
.xcdatamodeldis in Copy Bundle Resources and Compile Sources- Renamed
.xcdatamodeldand cleaned and closed Xcode project several times - Turned off and on the device
- Deleted folders from:
$ cd /Users/john/Library/Developer/Xcode/DerivedData - Changed sqlite name for forcing database generation
- Deleted (again) application from devine
I’ve been searching the solution for hours, and I still cannot find it.
Finally, after two days trying to solve this issue, I’ve found the solution here:
How to create the magic .xcdatamodeld folder / package?
I’m now finishing a project that other developer started, and it seems that he didn’t pushed the latest changes to the repo, but those changes were in the app in the device, and when I removed the app I deleted the right
.xcdatamodeldfile. The problem was that I had just aMyApp.xcdatamodelfile in the project, and this was the reason of having amomdempty folder, it seems.In order to create the right hierarchy of data model, the solution was quite easy:
MyApp.xcdatamodelAnd this embedded the
MyApp.xcdatamodelfile intoMyApp.xcdatamodeld. Now themomdfolder has themomfiles and the app runs OK. The only problem now is that I have twoMyApp.xcdatamodel, one with a green selected icon, but both with the same content so no problem.