Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create an NSPersistentStoreCoordinator with a nil model after deleting app from device

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:

  1. Change managedObjectModel instantiation with this __managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
  2. Cleaned Build Folder and Cleaned project
  3. Restarted Xcode
  4. Restarted computer
  5. Changed "momd" to "mom"
  6. .xcdatamodeld is in Copy Bundle Resources and Compile Sources
  7. Renamed .xcdatamodeld and cleaned and closed Xcode project several times
  8. Turned off and on the device
  9. Deleted folders from: $ cd /Users/john/Library/Developer/Xcode/DerivedData
  10. Changed sqlite name for forcing database generation
  11. Deleted (again) application from devine

I've been searching the solution for hours, and I still cannot find it.

like image 252
amb Avatar asked Sep 27 '12 08:09

amb


3 Answers

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:

  1. Select the MyApp.xcdatamodel
  2. Go to Editor > Add Model Version...

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.

like image 138
amb Avatar answered Sep 20 '22 03:09

amb


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"];
like image 24
Brayan Acebo Avatar answered Sep 20 '22 03:09

Brayan Acebo


 NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"select xcdatamodeld" withExtension:@"momd"];

select exact url for resource name..

like image 42
Bajaj Avatar answered Sep 18 '22 03:09

Bajaj