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:
__managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
.xcdatamodeld
is in Copy Bundle Resources and Compile Sources.xcdatamodeld
and cleaned and closed Xcode project several times$ cd /Users/john/Library/Developer/Xcode/DerivedData
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 .xcdatamodeld
file. The problem was that I had just a MyApp.xcdatamodel
file in the project, and this was the reason of having a momd
empty folder, it seems.
In order to create the right hierarchy of data model, the solution was quite easy:
MyApp.xcdatamodel
And this embedded the MyApp.xcdatamodel
file into MyApp.xcdatamodeld
. Now the momd
folder has the mom
files and the app runs OK. The only problem now is that I have two MyApp.xcdatamodel
, one with a green selected icon, but both with the same content so no problem.
I backed to build my first iOS app again. Today, I got this error:'Cannot create an NSPersistentStoreCoordinator with a nil model'. Actually, that is easy to fix. Please make this line of code:
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"[name]" withExtension:@"momd"];
The [name] you filled in is same as your model file (.xcdatamodeld). For example I got a TipRecord.xcdatamodeld then this line should be:
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"TipRecord" withExtension:@"momd"];
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"select xcdatamodeld" withExtension:@"momd"];
select exact url for resource name..
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