could not locate an NSManagedObjectModel for entity name

前端 未结 7 2511
[愿得一人]
[愿得一人] 2020-12-18 19:15

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 cr

相关标签:
7条回答
  • 2020-12-18 19:27

    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.

    0 讨论(0)
  • 2020-12-18 19:30

    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.
    0 讨论(0)
  • 2020-12-18 19:31

    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"]); 
    
    0 讨论(0)
  • 2020-12-18 19:31

    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.

    0 讨论(0)
  • 2020-12-18 19:32

    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.

    0 讨论(0)
  • 2020-12-18 19:32

    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?

    0 讨论(0)
提交回复
热议问题