6

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.

amb
  • 4,798
  • 6
  • 41
  • 68
  • Does it work on the simulator? Are you able to open the `.xcdatamodeld` file in Xcode? – FluffulousChimp Sep 27 '12 at 09:10
  • Alan, I'm able to open the `.xcdatamodeld` file in Xcode, but the application is not working in the simulator. It launches, but only displays a black screen. – amb Sep 27 '12 at 09:22
  • I think I've found something... I've just realized that I had not deleted all the files living in `/Users/john/Library/Developer/Xcode/DerivedData`, so when I did this the simulator went OK, even the managedObjectModel. Is there any way to delete these files in the device? – amb Sep 27 '12 at 09:31
  • 2
    Deleting the app from the device should remove the files. – FluffulousChimp Sep 27 '12 at 09:34
  • Now the app launches in the simulator (only if I delete DerivedData and Reset Content and Settings) but I'm getting the same `managedObjectModel` error. – amb Sep 27 '12 at 10:54
  • if the file exists at the url, then best guess is corrupted model. can you revert to prior revision in version control? – FluffulousChimp Sep 27 '12 at 11:18
  • So what you should do is create a new demo project (single view controller). Copy your model to that project, and add the above code. Run it a few times to be sure it fails like your main project. Zip it up and put on DropBox, then update your Question with a link to the project. – David H Sep 27 '12 at 12:13
  • It seems that there is no `.mom` file inside the `.momd` directory. Also, there is no `.xcdatamodeld` and it seems that Xcode is not finding the `.xcdatamodel` that exists in the project. I'll try to solve the issue adding an `.xcdatamodeld`. – amb Sep 27 '12 at 13:30
  • I've just posted the solution. The problems with the simulator were Xcode issues, I've just updated to 4.5 and everything regarding the iPhone Simulator works fine. – amb Sep 27 '12 at 14:15

4 Answers4

15

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.

Community
  • 1
  • 1
amb
  • 4,798
  • 6
  • 41
  • 68
2

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"];
1
 NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"select xcdatamodeld" withExtension:@"momd"];

select exact url for resource name..

Bajaj
  • 859
  • 7
  • 17
0

it's relatively simple.

if you have an xcdatamodel file without versions: use the extension "mom" in your managedObjectModel declaration. If your xcdatamodel has versions, you should use "momd".

Mario Gil
  • 35
  • 5