could not locate an NSManagedObjectModel for entit

2020-02-11 21:43发布

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.

7条回答
虎瘦雄心在
2楼-- · 2020-02-11 22:09

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.
查看更多
该账号已被封号
3楼-- · 2020-02-11 22:17

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.

查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-02-11 22:18

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.

查看更多
老娘就宠你
5楼-- · 2020-02-11 22:18

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.

查看更多
Rolldiameter
6楼-- · 2020-02-11 22:18

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

查看更多
我命由我不由天
7楼-- · 2020-02-11 22:19

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"]); 
查看更多
登录 后发表回答