ZyusAn
ZyusAn

Reputation: 379

"Cannot load NSManagedObjectModel. nil is an illegal URL parameter"

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...

Upvotes: 13

Views: 12302

Answers (6)

codaman
codaman

Reputation: 226

try "mom" instead of "momd"

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

Upvotes: 0

Rick Schlueter
Rick Schlueter

Reputation: 141

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.

Upvotes: 0

Simone
Simone

Reputation: 37

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.

Upvotes: -2

Tom Giraudet
Tom Giraudet

Reputation: 265

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

Upvotes: 5

garg
garg

Reputation: 2727

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

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

Upvotes: 0

Steve Wilford
Steve Wilford

Reputation: 9002

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.

Upvotes: 26

Related Questions