Cannot create NSPsistentStoreCoordinator with null model after uninstalling application from device

I get the error 'Cannot create an NSPersistentStoreCoordinator with a nil model' after uninstalling the application from the device. I am testing an iPhone application on an iPad. I put this code to check if I have a 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] returns nil. I have done the following things without success:

  • Modify the creation of the managedObjectModel instance with this __managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];
  • Cleaned up folder and cleaned up project
  • Rebooted Xcode
  • Rebooted computer
  • Changed "momd" to "mom"
  • .xcdatamodeld is in copy resources and compilation sources
  • Renamed .xcdatamodeld and cleared and closed the Xcode project several times
  • The device is turned off and on
  • Deleted folders from: $ cd /Users/john/Library/Developer/Xcode/DerivedData
  • Changed sqlite name to force database generation
  • Remote (again) application from devine

I have been looking for a solution for hours, and I still cannot find it.

+6
source share
4 answers

Finally, two days later, trying to solve this problem, I found a solution here:

How to create a magic folder / package .xcdatamodeld?

Now I'm finishing a project that another developer started, and it seems that he did not push the latest changes to the repo, but these changes were in the application on the device, and when I deleted the application, I deleted the right .xcdatamodeld file. The problem was that I only had the MyApp.xcdatamodel file in the project, and this was the reason for the presence of the empty momd folder.

To create the correct hierarchy of the data model, the solution was quite simple:

  • Choose MyApp.xcdatamodel
  • Go to Editor> Add Model Version ...

And that MyApp.xcdatamodel file in MyApp.xcdatamodeld . Now momd folder has mom files and the application works fine. The only problem is that I have two MyApp.xcdatamodel , one with a green icon selected, but both with the same content, so no problem.

+15
source

I again supported the new iOS app. Today I got this error: "Unable to create NSPsistentStoreCoordinator with nil model." This is actually easy to fix. Enter the following line of code:

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

The [name] you filled in is the same as your model file (.xcdatamodeld). For example, I got TipRecord.xcdatamodeld, then this line should be:

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

select the exact url for the resource name ..

+1
source

it is relatively simple.

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

0
source

Source: https://habr.com/ru/post/926384/


All Articles