Cocoa-Touch, Core Data: Getting exception when trying to insertNewObjectForEntityForName:inManagedObjectContext:
I'm following this how-to to implement Core Data storage in my app:
I have a
Model.xcdatamodel which defines a
Something model. I've used XCode to generate a class for that model.
I've imported the class in my .m file where I'm trying to:
Something* s = (Something *)[NSEntityDescription insertNewObjectForEntityForName:@"Something" inManagedObjectContext:managedObjectContext];
But this causes the following error:
2009-10-13 21:18:11.961 w9a[4840:20b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Something''
Am I missing something?
Personally, I prefer the following method:
// With some NSManagedObjectContext *context NSEntityDescription *desc = [NSEntityDescription entityForName:@"Something" inManagedObjectContext:context]; Something *s = [[[Something alloc] initWithEntity:desc insertIntoManagedObjectContext:context] autorelease];
I've noticed it's less prone to random Core Data errors, and is easier to debug. It's effectively doing the same thing as your code, but explicitly gets an entity description first, so you can debug that separately if need be.
Seems that you dont have a NSManageObject named "Something" in your object model...are you making your entity in the object model? I am not sure if you need to generate the code as well, but you can have xcode do that for you automatically by clicking on the entity, saying new, and selecting Managed Object from the menu there
Found my problem, the NSManagedObjectContext was not geting initialized properly for some reason. I've re-written that code following the how-to and now it seems to work.
Thanks anyway :)