1

I have a child view controller that requires the managedObjectContext be set on it in order to operate properly, and while I considered using a singleton this question seems to point strongly to that it should be passed around, starting with the AppDelegate.

I don't know how to pass it to a child view controller, however (note that it's set up via Storyboards). I tried a few ways:

  1. In prepareForSegue, before the embed segue is called, I save a reference to the destination view controller in the containing view controller as a property, then try to access this property, but in didFinishLaunching the segue obviously hasn't been set up yet.
  2. Accessing the childViewControllers property of the containing view controller, but again, at didFinishLaunching this is not populated.

So despite the Core Data template and examples seeming to indicate that the managedObjectContext of other view controllers should be set in didFinishLaunching, I see no way to.

How would I properly pass the managedObjectContext onto the view controllers that need it?

Community
  • 1
  • 1

3 Answers3

0

Add an NSManagedObjectContext property to the public API of your root controller, then set it to the managed object of your app delegate in didFinishLaunching. You can then use the context in your root view controller to pass it along to subsequent view controllers. In didFinishLaunching:

MyRootViewController *controller = (MyRootViewController *)self.window.rootViewController;
controller.context = self.managedObjectContext;

You now have a managedObjectContext in your root view controller that can be passed in the prepareForSegue method to the child view controller (the child view controller must have a public managedObjectContext property as well that you can access in the prepareForSegue method).

Steve
  • 1,840
  • 17
  • 20
0

The debate as to whether to have a single, globally accessible object, either as a property on AppDelegate or as a statically accessible Singleton, is largely a religious debate. There is no right or wrong answer and there are pros and cons either way. Do what is simplest and most natural to you. Typically, managedObjectContext is a de facto singleton: there is only one for the application and that one is used everywhere when needed. Your inclination to use a singleton or always access it from the AppDelegate maybe the way to go. Otherwise, and to answer you question, set it as a property on the view controller you are transitioning to before pushing the view controller or in prepareForSegue as you describe. To start the process, the root view controller could either instantiate it or access it from the AppDelegate in it's init, loadView, or viewDidLoad methods and keep a reference to it. For me the decision comes down to which approach is more obvious months or years later when I read the code and which approach do I want to maintain.

Heliotropix
  • 473
  • 1
  • 3
  • 8
0

If the NSManagedObjectContext was setup in AppDelegate, you don't pass it; rather, you create a reference to it:

AppDelegate *appDel = [UIApplication sharedApplication].delegate;   
NSManagedObjectContext *context = appDel.managedObjectContext;

The type of object you reference it from is irrelevant.

James Bush
  • 1,485
  • 14
  • 19