20

This is the code for toggleAddProject method, the Core Data code is almost the same as found in Apple's CoreDataBooks sample, however when I click the add button the app crashes with entityForName: could not locate an NSManagedObjectModel for entity name 'Project' on the line starting with newProjectController.project

-(IBAction)toggleAddProject 
{
    NewProjectViewController *newProjectController = [[[NewProjectViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease];

    // Create a new managed object context for the new project -- set its persistent store coordinator to the same as that from the fetched results controller's context.
    NSManagedObjectContext *addingContext = [[NSManagedObjectContext alloc] init];
    self.addingManagedObjectContext = addingContext;
    [addingManagedObjectContext setPersistentStoreCoordinator:[[fetchedResultsController managedObjectContext] persistentStoreCoordinator]];
    newProjectController.project = (Project *)[NSEntityDescription insertNewObjectForEntityForName:@"Project" inManagedObjectContext:addingContext];
    [addingContext release];


    UINavigationController *addNewNavigationController = [[UINavigationController alloc] initWithRootViewController:newProjectController];
    [self.navigationController presentModalViewController:addNewNavigationController animated:YES];  
    [addNewNavigationController release];
}

Everything has been synthesized, the Project entity exists. I can't figure out why it crashes. Most people seem to be able to fix this error by inserting the following code either in the method itself, or in viewDidLoad:

if (managedObjectContext == nil) 
{ 
    managedObjectContext = [(CoreDataBooksAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
}

When modified for my app delegate it makes no difference. Thanks for any help.

WrightsCS
  • 50,551
  • 22
  • 134
  • 186
ChrisJP
  • 975
  • 1
  • 7
  • 15

7 Answers7

40

This error has only a few possible sources:

  1. Typo in the Entity name.
  2. Nil managed object context object.
  3. Failure to add the model containing the entity to the persistent store the context uses.
  4. Failure to add the correct persistent store to the context itself.
TechZen
  • 64,370
  • 15
  • 118
  • 145
  • I ended up approaching this differently and this error doesn't appear now. But thanks anyway, there were no typos in my code but I'm sure it was one of the other things you listed, most likely the moc being nil. – ChrisJP Jul 29 '10 at 19:15
  • 4. was the problem in my case – race_carr Mar 05 '14 at 20:07
23

I had this problem when I had several different NSManagedObjectContexts. The quick way to debug it was to inspect the different connection bits and make sure my entity was listed before calling the context.

NSLog(@"Context: %@",context);
NSLog(@"PS Coord : %@",context.persistentStoreCoordinator);
NSLog(@"MOM : %@", context.persistentStoreCoordinator.managedObjectModel);
NSLog(@"Entities : %@",[[context.persistentStoreCoordinator.managedObjectModel entities] valueForKey:@"name"]); 
ghostfly
  • 728
  • 5
  • 12
  • 2
    By far the best solution for figuring out the problem. This just help me find out that my persistentStoreCoordinator isn;t hooked up properly. – ArtSabintsev May 15 '12 at 19:13
  • 1
    Bingo!! I haven't solved my problem yet, but this is certainly leading me down the right path. – Rickster Jan 14 '14 at 22:33
3

Use the debugger and confirm that your model is not nil. That is the most common cause of this error. If it is not nil then look for a typo in the entity name.

Stunner
  • 12,025
  • 12
  • 86
  • 145
Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
2

The Apple docs give some good information on debugging the error entityForName: could not locate an NSManagedObjectModel for entity name 'Foo'.

Look at this section of the Core Data Programming Guide.

Jamison Dance
  • 19,896
  • 25
  • 97
  • 99
1

Ok I ran across this issue as well and I solved it thusly. The original code was given as:

Event *event = (Event *)[NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:managedObjectContext];

While the code is concise it seems like the debugger can't display more detailed information about where the error is since you are both creating and configuring a new instance of the 'Event' entity (or whatever your Entity is named).

Instead I broke out this into three lines and the debugger displayed a lot more information:

Event *event = [[NSManagedObject alloc] init];
NSManagedObjectContext *moc = [self managedObjectContext];
event = [NSEntityDescription insertNewObjectForEntityForName:@"Event" inManagedObjectContext:moc];

I found I had not set the correct Type for one of the attributes and I had a typo in my code, all of which the debugger pointed out.

Boojeboy
  • 79
  • 7
0

During my development, I could not find Entities that I added later on. What worked for me: (Basically a sanity-tip)

Uninstall the App EVERY TIME you change the Data Model!

The Data Model is cached by Core Data between installs, to make sure the integrity stays in tact. Delete the App from the simulator / iPhone to be able to test your changes.

PS: does anyone know how to do that automatically?

Luc Bloom
  • 1,120
  • 12
  • 18
  • You're supposed to use lightweight migrations to resolve this. Here's a short blog post explaining this: http://blog.the-nerd.be/2012/02/how_to_do_a_lightweight_core_data_migration/ Here's a more detailed blog post: http://www.raywenderlich.com/27657/how-to-perform-a-lightweight-core-data-migration – Liron Yahdav Jul 31 '14 at 17:31
  • "Every time you update something in your model, you need to create a new version." Yeah.. no. I'd rather delete the existing one on the device every time (during initial development). This way, I don't have to think about wether the change I just made was lightweight enough. This way, I have a workflow that _always_ works. – Luc Bloom Aug 07 '14 at 07:15
-1

TechZen is spot on...in my case it was #4. Walk through the steps in the following link and this should help you add the appropriate CoreData methods into an existing project and get everything set up correctly so you don't run into the error you're having.

Adding Core Data To Existing iPhone Projects

shaunpickford
  • 106
  • 1
  • 7
  • 1
    Here is a working link: http://wiresareobsolete.com/wordpress/2009/12/adding-core-data-existing-iphone-projects/ – Matt Miller Aug 08 '12 at 15:36