Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoreData error: +entityForName: could not locate an NSManagedObjectModel for entity name

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
like image 465
marsalal1014 Avatar asked Aug 17 '11 22:08

marsalal1014


2 Answers

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.

like image 171
TechZen Avatar answered Oct 27 '22 09:10

TechZen


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?

like image 39
Luc Bloom Avatar answered Oct 27 '22 09:10

Luc Bloom