Search code examples
ioscore-dataios6

'+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name


I'm trying to save a record in a table. I read all the questions and answers in StackOverflow but I couldn't fix the problem. When I try to save data, I get the error:

2013-08-31 22:25:37.321 OnTheRoadV2[26581:11603] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Trip''
*** First throw call stack:
(0x18e7012 0x1427e7e 0x743e7 0xba402 0x41db 0x143b705 0x372920 0x3728b8 0x433671 0x433bcf 0x432d38 0x3a233f 0x3a2552 0x3803aa 0x371cf8 0x2404df9 0x2404ad0 0x185cbf5 0x185c962 0x188dbb6 0x188cf44 0x188ce1b 0x24037e3 0x2403668 0x36f65c 0x29ed 0x2915)
libc++abi.dylib: terminate called throwing an exception

My code:

- (IBAction)start:(id)sender {
    NSManagedObjectContext *context = [self managedObjectContext];

    // Create a new managed object
    NSManagedObject *newTrip = [NSEntityDescription insertNewObjectForEntityForName:@"Trip" inManagedObjectContext:context];
    [newTrip setValue:self.name.text forKey:@"name"];

    NSError *error = nil;
    // Save the object to persistent store
    if (![context save:&error]) {
        NSLog(@"Can't Save! %@ %@", error, [error localizedDescription]);
    }
        _status.text = @"Trip started!";


}

- (NSManagedObjectContext *)managedObjectContext {
    NSManagedObjectContext *context = nil;
    id delegate = [[UIApplication sharedApplication] delegate];
    if ([delegate performSelector:@selector(managedObjectContext)]) {
        context = [delegate managedObjectContext];
    }
    return context;
}

Solution

  • use magical record if you are new to core data. its easy to integrate: here is atutorial: http://ablfx.com/blog/article/2