1

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;
}
Lücks
  • 3,806
  • 2
  • 40
  • 54

2 Answers2

4
[self managedObjectContext];

is returning nil, there isn't much more I can tell you from your posted code.

Kevin
  • 16,696
  • 7
  • 51
  • 68
  • Sorry, I'm newbie on iOS. If my context is returning 0x0000000, this means nil? – Lücks Sep 01 '13 at 01:39
  • Thanks. I added a xcdatamodel to my project and added a Entity. And then, I wrote this code. Something is missing? – Lücks Sep 01 '13 at 01:41
  • Creating a managed object context is a long process, but to see how it is done, create a new project > master detail application > and make sure "Use Core Data is checked". Look in the appdelegate there for example code. – Kevin Sep 01 '13 at 01:43
  • Hey Peeps, Can someone answer this - http://stackoverflow.com/questions/26890278/entityforname-nil-is-not-a-legal-nspersistentstorecoordinator-for-searching-f – Amresh Nov 12 '14 at 15:32
2

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

Jatin
  • 1,668
  • 2
  • 16
  • 23
  • awesome! I followed this tutorial: http://yannickloriot.com/2012/03/magicalrecord-how-to-make-programming-with-core-data-pleasant/#sthash.GNErDSr8.8VBBav02.dpbs – Lücks Sep 01 '13 at 19:14