Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share a ManagedObjectContext when using UITabBarController

I have an iPhone application that has a MainWindow.xib holding a UITabBarController, which in turn has a UINavigationController and a custom UIViewController subclass in its ViewControllers array. The root view controller for the UINavigationController and the custom view controller are both loaded from other xib files.

The app uses core data, the stack is initialized in the app delegate (as per the convention).

The app delegate adds the UITabBarController to the window:

- (void)applicationDidFinishLaunching:(UIApplication *)application {        
    // Configure and show the window
    [window addSubview:[tabBarController view]];
    [window makeKeyAndVisible];
}

I realize that I need to propagate a pointer to the ManagedObjectContext created in the app delegate, but I don't know how to proceed (even reading all the good commentary on the topic here and here):

  • Do I propagate the ManagedObjectContext to the UITabBarController and from there on to the individual view controllers and if so, how?
  • Or do I propagate the ManagedObjectContext directly to the root view controller of the UINavigationController and to the custom view controller and how would I do that?

I guess I don't understand well enough how to work with the UITabBarController.

like image 674
mvexel Avatar asked Jan 15 '10 09:01

mvexel


4 Answers

Ideally you want to pass either the NSManagedObjectContext, NSFetchedResultsController or the relevant NSManagedObject "down" into the UIViewController. This allows the "parent" to control the "child" and determine what the child should have. This creates a more loosely coupled design and allows you to easily re-arrange UIViewController instances as needed. It also makes it easier to reuse a UIViewController.

In a tab view design it is no different. Your AppDelegate passes the NSManagedObjectContext to whoever is responsible for creating the initial UIViewController instances that go into the UITabBarController. In turn that creator passes the relevant information (NSManagedObject, NSFetchedResultsController, and/or NSManagedObject instances) into the UIViewController instances as it is constructing them.

like image 167
Marcus S. Zarra Avatar answered Nov 15 '22 00:11

Marcus S. Zarra


If you want to use the dependency injection method to pass the managed object context with a tab bar controller, a more robust solution would be to loop on all the view controllers in applicationDidFinishLaunching:

for (id vc in tabBarController.viewControllers) {
    [vc setManagedObjectContext:self.managedObjectContext];
}
like image 36
gerry3 Avatar answered Nov 14 '22 23:11

gerry3


Good, I looked long and hard at the CoreDataBooks sample application and did it like this:

  • Created IBOutlets to the RootViewController (the top view controller of the UINavigationController) and the MapViewController (the custom view controller) in the app delegate.
  • Connected the outlets to the view controllers in the MainWindow.xib
  • Added the following code to applicationDidFinishLaunching:

    // pass the managed object context to the view controllers
    RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
    rootViewController.managedObjectContext = self.managedObjectContext;
    
    mapViewController.managedObjectContext = self.managedObjectContext;
    

And now it works like a charm.

like image 33
mvexel Avatar answered Nov 14 '22 22:11

mvexel


I've ran into this same problem, i'll share my solution.

First you need a reference to the Nav Controller in the Tab Bar in the nib file, make sure you connect it up.

IBOutlet UINavigationController *navigationController;

Then, get the Controller as recommended in the support docs and send it the managedObjectContext:

SavedTableViewController *saved = (SavedTableViewController *)[navigationController topViewController];
saved.managedObjectContext = self.managedObjectContext;

Alex (from another post) is right, "You should generally stay away from getting shared objects from the app delegate. It makes it behave too much like a global variable, and that has a whole mess of problems associated with it."

like image 1
W Dyson Avatar answered Nov 14 '22 23:11

W Dyson