CoreData: 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an entity named 'DocumentLocations' in this model.'
I've s开发者_高级运维uddenly been tripped up by a bizarre issue. I have a rather extensive CoreData data model, and I've copied it (and the project containing it) over to a new project for a prototype that I'm working on. I got it working, and was mixing and matching new entities and attributes.
Suddenly, and for no apparent reason, I'm suddenly getting the following error:
'NSInternalInconsistencyException', reason: '+entityForName: could not locate an entity named 'DocumentLocations' in this model.'
I dug into the whole process for creating the NSManagedObjectContext, NSPersistentStoreCoordinator, and NSManagedObjectModel, and have gotten to the point where it's pretty clear that the object model is not being loaded. I execute the following command:
_managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];
But when I look for the entities in the model after loading (this method creates the model from the main bundle if you pass nil to mergedModelFromBundles), nothing's there.
It appears that my xdatamodel file is not being seen by Xcode or the Simulator. And I have absolutely no idea why not. I'm going to try to force the issue by explicitly loading it through a URL, but I'm not sure how that's going to work.
Any clues? CoreData issues can be absolutely baffling. I've managed to work my way through most of them over time, but this one's a head-scratcher.
Edit: Alex figured it out. I'm only left with wondering how the model got removed from the 'Compile Sources' list in Xcode. Wish Xcode were more reliable. Can really be a PITA.
@Alex figured it out. For some reason, the xdatamodel was removed from the build settings. I added it back in and it worked. I don't really understand why it would have been removed in the first place. It literally disappeared between one build and the next.
Fixed this by deleting the app - from the simulator and my iPhone.
Apparently the DB was added without the entities in an early run. Then, once I added the entities and wired everything up, started receiving an error similar to above...
Deleting the app allowed the DB to be recreated and fixed the problem.
精彩评论