Search code examples
iosnsmanagedobjectcontext

nil is not a legal NSManagedObjectContext parameter


I am getting the following error when calling core data

   Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'ExcerciseInfo''

The class I am using works on another uitableview but does not work with the current one. I have pinpointed the point where the error is thrown:

   if (_fetchedResultsController != nil) {
        return _fetchedResultsController;

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription
                                   entityForName:@"ExcerciseInfo" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

I have looked at other similar questions but don't highlight any relavent fixes. I presume im not passing the context across as it is not the first view controller and is a second tab.

Currently in my appdelagate I am calling this function:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    UITabBarController *tbc = (UITabBarController *)self.window.rootViewController;
    UINavigationController *navigationController = tbc.viewControllers[0];
    FBCDMasterViewController *controller = (FBCDMasterViewController *)navigationController.topViewController;
    controller.managedObjectContext = self.managedObjectContext;

    return YES;
}

The additional uitableview controller I am targeting is FBCDRoutineViewController

Any suggestions? I hope this is clear.


Solution

  • As stated by other users. I did not call the context across on the new view controller.

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