views:

140

answers:

3

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?

+1  A: 

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

Daniel
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
thats what i was asking
Daniel
thats odd, usually when i see that error is just mispelling on the entity name or something like that
Daniel
are you sure that the xcdatamodel is associated to the object context you are using ?
Daniel
+4  A: 

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.

Tim
A: 

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

Prody