NSEntityDescription entityForName returning nil
I did some changes to my model (but I don't want migration yet, so I just remove the application, built clean etc.)
However, when I run it on the iPhone or in the simulator, I get the following error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'executeFetchRequest:error: A fetch request must have an entity.'
I set the entity like this:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Document" inManagedObjectContext:manag开发者_JAVA技巧edObjectContext];
My managedObjectContext is not nil. But I suspect that it doesn't load the object model correctly or something similar because If I display the entities in the model, the list is empty:
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
NSLog(@"%d", [[managedObjectModel entities] count]);
How can I make sure the model is loaded?
Where are you "displaying the entities"? If they don't show up in the data model editor, they aren't there.
You can use -[NSManagedObjectModel entities]
to get an array of the entities that the model has.
In turn, you can use -[NSPersistentStore managedObjectModel]
to see what model is loaded and then query its entities.
Gotta tell you however, most of the time this error is caused by a spelling typo. That's why its good to use defines or class methods to return entity names.
I experienced the same problem. As suggested in TechZen's comment, the fix for me was to right-click on the file in Xcode 3.x, select "Get Info" and then tick the appropriate box under the "Targets" tab.
UPDATE: In Xcode 4 the "Get Info" menu option has been removed. Instead you can define "Target Membership" for files in the right sidebar.
Another thing to note is that, when you click on xcdatamodeld file to view entities and what not, under CONFIGURATIONS on the left side, Default configuration, you see the list of entities. If they're classes in your project (which I think they should be), make sure to put the class names under Class column.
Had the same problem. In my case the old model was still loading so I had to clean project and also uninstall the app from simulator.
To debug:
- In order to print out all the entities in the object model used this: NSDictionary* dictionary = [self.managedObjectContext.persistentStoreCoordinator.managedObjectModel entitiesByName]; [dictionary enumerateKeysAndObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(id key, id obj, BOOL *stop) { NSLog(@"%@ = %@", key, obj); }];
- Also used this to print out the number of entities: NSLog(@"Number of entities: %d", [[self.managedObjectContext.persistentStoreCoordinator.managedObjectModel entities] count]);
In my case:
- My
Model.xcdatamodeld
is first in a app (name A),then it works fine - I add modify the app to a static lib
- add step 2 static lib to a new app (name B)
I have the same problem as Kamchatka when use app B
You can try to move Model.xcdatamodeld
to main project to try, that works for me.
I experienced this issue & had a very bizarre fix. I had copy/pasted some code from a different project for the actual creation of the entity (& then changed the values for the entity & context):
NSEntityDescription *e = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:testContext];
.. which was, no matter what I checked, returning nil despite the Entity definitely existing in the context.
All I did was delete the line, change the variable name, & manually retype it:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:testContext];
I wish I could tell you why this fixed it - I'm still not sure. I had previously tried all the solutions posted to this page. I suspect this has something to do about "having multiple projects open" - so if you are in the same situation as me, try rewriting the datamodel loading/fetching process manually.
I have encountered this issue with multiple projects opened at the same time with Xcode 4.4.
If you have multiple projects, Xcode 4.4 may use the datamodel from the wrong project and not find your entities. Solution was to have only one project opened at a time.
精彩评论