简体   繁体   中英

Passing a managedObjectContext from the app delegate to a view controller

Using apple's core data recipe example application, I've learned how I can pass down my managedObjectContext from the app delegate to other view controllers. However, I'm trying to pass the managedObjectContext to a view controller that isn't in part of the tab bar controller, so I can use it for a pick list type view controller, and when I try to run this view controller, the value of the managedObjectContext keeps coming up as null, indicating that the context is not getting passed. I hope someone can enlighten me about the correct way to do this.

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

// pass down our managedObjectContext to our AssessmentListTableViewController
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UINavigationController *nav1Controller = tabBarController.viewControllers[0];

AssessmentListTableViewController *assessmentListVC = (AssessmentListTableViewController *)nav1Controller.topViewController;
assessmentListVC.managedObjectContext = self.managedObjectContext;

//pass down our managedObjectContext to our ClientListTableViewController
UINavigationController *nav2Controller = tabBarController.viewControllers[1];

ClientListTableViewController *clientListVC = (ClientListTableViewController *)nav2Controller.topViewController;
clientListVC.managedObjectContext = self.managedObjectContext;

//pass down managedObjectContext for use in the AddEpisodeInfo view controller
AddEpisodeInfo *addEpisodeInfoVC = [[AddEpisodeInfo alloc]init];
addEpisodeInfoVC.managedObjectContext = self.managedObjectContext;

//pass down manageObjectContext to a separate tableview controller which allows me to pick the assessment to use
PickAssessmentTableViewController *pickAssessmentTableVC = [[PickAssessmentTableViewController alloc]init];
pickAssessmentTableVC.managedObjectContext = self.managedObjectContext;

return YES;
}

The top two passes of the managedobjectcontext work properly, but the last one does not work, and I'm not sure why.

You should not allocate those viewControllers in the AppDelegate. Those are not the viewController you encounter later in your app. They are completely separate instances.

You want to pass the context wherever you naturally would create the viewController.

For example if you push a new viewController you set the context right where you create the viewController:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    PickAssessmentTableViewController *pickAssessmentTableVC = [[PickAssessmentTableViewController alloc]init];
    pickAssessmentTableVC.managedObjectContext = self.managedObjectContext;

    // setup...

    // and push
    [self.navigationController pushViewController:pickAssessmentTableVC animated:YES];
}

or if you use a segue, you set the context in prepareForSegue:sender: by accessing the destinationViewController of the segue:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"pushAddEpisodeInfo"]) {
        AddEpisodeInfo *addEpisodeInfoVC = (AddEpisodeInfo *)segue.destinationViewController;
        addEpisodeInfoVC.managedObjectContext = self.managedObjectContext;
    }
}

Yes, that means it's possible that you have to add a managedObjectContext property to viewControllers that won't actually use Core Data themselves. Just so you can pass the context to the next viewController. That is expected and not a problem.

You're instancing pickAssessmentTableVC in a variable thats only in the scope of the function.

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

As you don't have any other references to it ARC just releases it at the end of the function.

try putting

@property (nonatomic) pickAssessmentTableVC;

in your app delegate interface and change the line to

pickAssessmentTableVC = [[PickAssessmentTableViewController alloc]init];

Also it looks like you've got a lot of core data stuff going on, I would highly recommend using a helper such as Magical record - it will make your life a million times easier in the long run and you won't need to worry about keeping track of contexts at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM