13

I want to get the managed object context from AppDelegate, but the app crashed after I put the two lines of code into the method even I did nothing else, and there was a message in debug area:"CoreData: Cannot load NSManagedObjectModel. nil is an illegal URL parameter..."

The code added in my method:

AppDelegate *delegate = [UIApplication sharedApplication].delegate;
NSManagedObjectContext *managedObjectContext = delegate.managedObjectContext;

-managedObjectModel method in AppDelegate:

- (NSManagedObjectModel *)managedObjectModel {
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
    return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"FoodPin" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}

and the -managedObjectContext method:

- (NSManagedObjectContext *)managedObjectContext {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) {
    return _managedObjectContext;
}

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
    return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc]initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}

"FoodPin" is my project name.So what's wrong here?I'm new to iPhone programming (Core Data in particular).

Can anyone help me?

Thanks...

ZyusAn
  • 369
  • 1
  • 4
  • 13
  • The problem is this `[[NSBundle mainBundle] URLForResource:@"FoodPin" withExtension:@"momd"];` is returning nil into `modelURL`. Do you have a Core Data model named FoodPin in your project? – Steve Wilford Oct 06 '15 at 10:16
  • Oh..thanks a lot, I forgot to alter my name of .xcdatamodeld file! – ZyusAn Oct 06 '15 at 10:40

6 Answers6

26

The problem is this line:

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

modelURL is nil meaning that the system couldn't find the resource FoodPin.momd.

Make sure you have a Core Data model in your project named FoodPin. It will appear as FoodPin.xcdatamodeld in the Project Navigator.

Steve Wilford
  • 8,894
  • 5
  • 42
  • 66
5

I had the same problem but for me the modelURL was correctly set. The problem was that my *.xcdatamodeld file was not in the Copy bundle ressources anymore. I don't know why it disappear but to add it again fix the problem.

Here is how to fix it : You project > Build Phases > Copy Bundle ressources > "+" button and select you xcdatamodeld file

Tom Giraudet
  • 265
  • 1
  • 4
  • 9
  • This solved my problem. I wonder what happened. Looking at my commit history, the *. xcdatamodeld was never in the Copy Bundle Resources build phase. I'm using Xcode 8 beta 6. – Stian Høiland Aug 23 '16 at 11:35
  • I removed the *. xcdatamodeld from the Copy Bundle Resources build phase, and the problem remains solved. Probably a caching issue with Xcode, which means that doing a Clean or Clean Build Folder might solve this issue for others. – Stian Høiland Aug 23 '16 at 11:45
0

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

Make sure your data model name match with URLForResource:XYZ.

garg
  • 2,651
  • 1
  • 24
  • 21
0

OK here's what I had to do. I had reverted by iOS version back to 9.x in order to run on an older iPad I have. This caused the error as prior versions must be case-sensitive in the bundle.

Rick Schlueter
  • 141
  • 1
  • 10
0

try "mom" instead of "momd"

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

codaman
  • 206
  • 4
  • 6
-2

I had the same error.

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

"n" in name had to be "N": like everything else in obj c this mother is case sensitive.

pacholik
  • 8,607
  • 9
  • 43
  • 55
Simone
  • 37
  • 3