Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insertNewObjectForEntityForName:

I set up an Entity using the Xcode .xcdatamodel file editor. I created an entity called Person, added a few attributes, then generated a .m file to represent it. That all works fine.

Now when I go to write a line of code like:

  Person * person = (Person*)[NSEntityDescription
                        insertNewObjectForEntityForName:@"Person"
                        inManagedObjectContext:managedObjectContext];

And I get:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'Person''

I followed the Location example exactly though, step-for-step I believe, but I think I must have missed some kind of crucial "registration" step where I tell Xcode that my Person entity should be accessible.. Also I didn't have a way to "initialize" the managedObjectContext at all, the Location example doesn't seem to do that either.

like image 642
bobobobo Avatar asked Nov 23 '09 03:11

bobobobo


2 Answers

The fact that you didn't set up the MOC is almost certainly the problem. Most specifically, it means you're probably not loading your MOM (Managed Object Model) that defines Person. Somewhere in your code you should have something like this:

managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    

And something like this:

persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

And something like this:

NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
    managedObjectContext = [[NSManagedObjectContext alloc] init];
    [managedObjectContext setPersistentStoreCoordinator: coordinator];

I'm just copying lines out of the AppDelegate of the Core Data template (what you get if you make a new application that uses Core Data).

If you have all that, make sure that your xcdatamodel is listed in your Compile Sources step of the build. And of course make sure that Person is actually the Entity name in your xcdatamodel. Entity name is not the same as Class, though they are often set to be the same.

like image 69
Rob Napier Avatar answered Sep 19 '22 16:09

Rob Napier


You need the init of the Core Data

-(void)initCoreData{
        NSError *error;
        //Path to sqlite file.
        NSString *path = [NSHomeDirectory() stringByAppendingString:@"/Documents/Level4.sqlite"];
        NSURL *url = [NSURL fileURLWithPath:path];

        //init the model
        NSManagedObjectModel *managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:nil];

        //Establish the persistent store coordinator
        NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel];

        if(![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]){

                NSLog(@"Error %@",[error localizedDescription]);

        }else{
                self.context = [[[NSManagedObjectContext alloc ] init ] autorelease];
                [self.context setPersistentStoreCoordinator:persistentStoreCoordinator];
        }

        [persistentStoreCoordinator release];
}
like image 21
andyhu Avatar answered Sep 21 '22 16:09

andyhu