views:

87

answers:

2

Hi geniuses out there,

I am well into building a Core Data tab-based iPad application. I am passing in my NSManagedObjectContext to my root view using the following in my app delegate class.

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    self.rootViewController.managedObjectContext = self.managedObjectContext;
    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];

    return YES;    
}

My question is: how do I set the same managedObjectContext on all my tabs? It would also be good if I could set up some of my service classes in the app delegate and use the same instance across all my tabs. How can this be done?

Thanks in advance!

+1  A: 

A "tab" is simply another view controller. As you initiate the VCs for each tab, you can hand them a managed object context in exactly the same way you set rootViewController.managedObjectContext, assuming that they have managedObjectContext properties.

Some people use singleton objects to provide Core Data objects to their classes; what I did in the app I'm currently working on was to declare a protocol, , with getters for my NSManagedObjectContext, NSManagedObjectModel, and NSPersistentStoreCoordinator, and implemented that protocol on my appDelegate. My view controllers that need to use core data have member variables of type NSObject , and as they create each other they set the property. They're all actually pointing to my appDelegate, but they don't know it, so they're not tightly coupled to an upstream object.

Seamus Campbell
I can't see how/where each VC is being initialised for each tab. To add a tab I just add another thing under the tab controller and point it to a VC.
Mike Simmons
You don't need their init, you can handle it in the `-applicationDidFinishLaunching...` method of your AppDelegate. Grab the `UITabbarController` and iterate over its `-viewControllers`.
Marcus S. Zarra
Marcus -- yeah, I meant in -applicationDidFinishLaunching -- when I've used tab controllers, I've created and added the tabs manually. Either way, I agree with you that that is the right place for DI.
Seamus Campbell
Thanks very much for your answers!
Mike Simmons
+1  A: 

The simplest solution is to add super class for you tab view controllers with a managedObjectContext attribute and a custom getter method like:

- (NSManagedObjectContext *) managedObjectContext{
    if (managedObjectContext !=nil) {
        return managedObjectContext;
    }
    MyAppDelegateClass *appDelegate=(MyAppDelegateClass *)[[UIApplication sharedApplication] delegate];
    self.managedObjectContext=appDelegate.managedObjectContext;
    return managedObjectContext;
}

If all your tab view controllers inherit this method they will all automatically find the app delegate's managed object context. And you're done.

TechZen
We still disagree on the use of Singletons :) I find that dependency injection when the application launchs is a better solution.
Marcus S. Zarra
@Marcus S. Zarra -- Dependency injection may well be better but it's not simpler for a novice to implement. I find novices have trouble tracking the view controller hierarchy and this gets around that problem especially in the case of tab bars.
TechZen
I have always been a fan of hitting the deep end first :)
Marcus S. Zarra
Great answers guys! I flipped a coin and marked the dependency injection answer as the accepted one.
Mike Simmons