9

I've been struggling with CoreData for a few days, but I keep getting this error:

'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name.

I have checked the entity name and what I wrote on my code and they're the same. I also recreated the object data-model and even delete the app from the simulator but nothing seems to fix it. Here's what I have:

method to save into CoreData:

-(IBAction)save:(id)sender {
    NSManagedObject * newNews = [NSEntityDescription insertNewObjectForEntityForName:@"NewsStand"
    inManagedObjectContext:coredata.managedObjectContext];
    [newNews setValue:news_title forKey:@"story_title"];
    [newNews setValue:news_desc forKey:@"story_desc"];
    [newNews setValue:news_image  forKey:@"story_image"];
    [newNews setValue:test  forKey:@"story_url"];
    [coredata commit];
    NSLog(@"data saved!!!!");
}

I have implemented all methods of core data in a separated class:

applicationDocumentsDirectory,  
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator, 
- (NSManagedObjectModel *)managedObjectModel, 
- (NSManagedObjectContext *)managedObjectContext
Sujay
  • 2,510
  • 2
  • 27
  • 47
marsalal1014
  • 1,897
  • 3
  • 17
  • 24

6 Answers6

26

This is a fairly common error and it has three causes:

  1. Misspelling the entity name e.g. NewsStand instead of NewsStands.
  2. Having a nil managed object context
  3. Having no or the wrong managed object model loaded.

(1) is the most common but (3) is more common than (2). You can check that you are loading the right model with the keypath:

aManagedObjectContext.persistentStoreCoordinator.managedObjectModel.entities

then check the entity's names.

TechZen
  • 64,370
  • 15
  • 118
  • 145
  • Hi! I tried what you mentioned "aManagedObjectContext.persistentStoreCoordinator.managedObjectModel.entities" and I'm getting null! but after re-checking my code I'm sure that I wrote correctly my entity name, any idea how can i get this trough? or how assurance the objectContext is not nil – marsalal1014 Aug 18 '11 at 04:43
  • Hey @TechZen, thanks I already fixed. Added a method to check if the instance of the class were the coredata code resides is nil. – marsalal1014 Aug 18 '11 at 05:49
  • One variant of (3) that happened to me was that the wrong version of my data model was marked as "current", so the entity the app was looking at didn't exist in that version. – Joe Trellick Nov 13 '12 at 12:22
  • Ahh... this got me. I had a typo in my URLByAppendingPathComponent. It hadn't been updated when the model was renamed. Thanks. Hopefully this will help others too. I was seeing the exact same behavior. Looking at the NSManagedObjectModel and it had everything I was looking for, but they were always nil from the context. – GreenKiwi Apr 18 '13 at 04:07
2

I had a similar problem and found TechZen's answer to be helpful (especially the suggestion to check the entities). However, my problem turned out to be a variant of (2): I could see that the moc itself wasn't nil, but I hadn't set the persistent store coordinator.

[aManagedObjectContext setPersistentStoreCoordinator:persistentStoreCoordinator]

or similar.

I'd add this as a comment to TechZen's answer, but can't yet, and want to include it in case someone else has my problem.

sanmai
  • 29,083
  • 12
  • 64
  • 76
pbergson
  • 230
  • 3
  • 9
2

During my development, I could not find Entities that I added later on. What worked for me:

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
  • What about an app that has been released to the store? End-users should not have to reinstall to get the latest data model, correct? – Ngoan Nguyen Aug 22 '14 at 16:19
  • 1
    @NgoanNguyen Good point. Migration is unavoidable. http://www.raywenderlich.com/27657/how-to-perform-a-lightweight-core-data-migration – kubilay Sep 01 '14 at 17:02
  • I prefer resetting the simulator altogether. Made a custom shortcut key- `Cmd+Shift+K` same as we clean project in Xcode using this answer: http://stackoverflow.com/a/8352242/2082569 – atulkhatri Nov 09 '15 at 07:23
1

Ensure that coredata.managedObjectContext is not nil.

Mark Adams
  • 30,776
  • 11
  • 77
  • 77
0

Don't have the rep to comment - but Luc Bloom's response fixed MY issue. I completely forgot I changed some stuff in the data model after initial build/install and spent entirely too long banging my head against the desk.

tnd
  • 51
  • 7
0

If you're editing a framework and running a unit test to get the error, make sure your xcdatamodeld file is added to the test target. Frameworks behave differently than normal projects.

Jeff
  • 349
  • 2
  • 12