0
votes

I'm very new in iOS programming and trying to learn by sample codes combining. Would you please help me with a specific situation I may have, which caused a well documented error "+entityForName: nil is not a legal NSManagedObjectContext". None of the previous threads could help me out here. I have one tableview with 1. segue to detail and additional 2. segue to subtableview.

This is the error I received:

2014-04-25 17:18:41.150 PhotoLocations[27319:60b] run addEvent - APLEvent 2014-04-25 17:18:41.155 PhotoLocations[27319:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Event''

I guess, I do not pass the context across and the segue is insufficient. Since I'm newbie, I want to learn how to enable multiple segues from one TVC with CoreData?

This is how the segue method in the first TVC:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
        [[segue destinationViewController] setDetailItem:object];
    }
}

Thanks in advance for your help!

EDIT: Adding Debug NSLog reports:

2014-04-25 19:07:27.059 PhotoLocations[28268:60b] run viewDidLoad
2014-04-25 19:07:27.059 PhotoLocations[28268:60b] run locationManager
2014-04-25 19:07:27.062 PhotoLocations[28268:60b] run viewWillAppear
2014-04-25 19:07:27.062 PhotoLocations[28268:60b] run numberOfSectionsInTableView
2014-04-25 19:07:27.063 PhotoLocations[28268:60b] run numberOfSectionsInTableView
2014-04-25 19:07:27.064 PhotoLocations[28268:60b] run numberOfRowsInSection
2014-04-25 19:07:27.102 PhotoLocations[28268:60b] Location manager failed
2014-04-25 19:07:28.141 PhotoLocations[28268:60b] run addEvent
2014-04-25 19:07:28.141 PhotoLocations[28268:60b] run addEvent - CLLocation
2014-04-25 19:07:28.142 PhotoLocations[28268:60b] run locationManager
2014-04-25 19:07:28.144 PhotoLocations[28268:60b] Getting a random location
2014-04-25 19:07:28.145 PhotoLocations[28268:60b] run addEvent - APLEvent
2014-04-25 19:07:28.150 PhotoLocations[28268:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Event''
*** First throw call stack:
(
    0   CoreFoundation                      0x023461e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x019578e5 objc_exception_throw + 44
    2   CoreData                            0x01608a1b +[NSEntityDescription entityForName:inManagedObjectContext:] + 251

FINAL EDIT: I found the solution thanks to @svena within Issues handing over managedObjectContext

The correct method for prepare for segue is like below:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [super prepareForSegue:segue sender:sender];
    if ([[segue identifier] isEqualToString:@"segueNameOfPresentingYourModalViewController"] == YES) {
        UINavigationController *navigationController = segue.destinationViewController;
        YourCustomViewController *aController = (YourCustomViewController *)navigationController.topViewController;
        aController.managedObjectContext = self.managedObjectContext;
    }
    else if ([segue.identifier isEqualToString:@"segueNameOfPushingYourViewController"] == YES) {
        YourCustomViewController *aController = segue.destinationViewController;
        aController.managedObjectContext = self.managedObjectContext;
    }
}
1
I am not sure the problem is here. Try adding an exception breakpoint in Xcode and see where exactly it stops. - Leo Natan

1 Answers

0
votes

Your supposition is correct, the problem appears to be that you don't have a managed object context in your destination view controller. There are a few possible solutions:

  1. Pass the MOC when preparing for the segue (clean, lots to remember, may pass to a class that doesn't need it just to pass on to another that does.
  2. Get the MOC from the detail item (cheeky, not very transparent).
  3. Use a singleton owner for the core data stack and get the mor from there (clean, transparent, some people hate singletons...)