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?

13.10.2009 18:24:11
Are you sure your managedObjectContext has a persistentStoreCoordinator instance with a managed object model that contains Something entity? In other words, are you sure you've constructed the entire Core Data stack properly?
Barry Wark 13.10.2009 19:38:50

Personally, I prefer the following method:

// With some NSManagedObjectContext *context
NSEntityDescription *desc = [NSEntityDescription entityForName:@"Something" 
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.

13.10.2009 18:41:03

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

13.10.2009 18:35:49
I'm not sure I understand exactly what you're asking, but I confirmed I have the item in my model (the .xcdatamodel file opened in XCode), I also have the generated class, it's imported. I have also checked the spelling for the names, they are correct.
Prody 13.10.2009 18:38:08
thats what i was asking
Daniel 13.10.2009 18:43:01
thats odd, usually when i see that error is just mispelling on the entity name or something like that
Daniel 13.10.2009 18:43:32
are you sure that the xcdatamodel is associated to the object context you are using ?
Daniel 13.10.2009 18:44:53

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 :)

13.10.2009 18:41:25