In my application, I instantiate a Managed Object Context (moc) in the applicationWillFinishLaunchingWithOptions
method in my app delegate. During normal operation, I pass that same moc to all view controllers in my hierarchy via custom initWithMoc initialization methods I've set up for each view controller.
Everything works fine, but now I'm trying to use iOS6's built-in state restoration features.
When I get to the viewControllerWithRestorationIdentifierPath
restoration method in the view controller, I'm not sure how to access the newly created moc from the app delegate.
How do I point each view controller's moc to the same one from the app delegate?
According to Apple's iOS App Programming Guide: State Preservation and Restoration,
It is worth noting that if you specify a restoration class for a view controller, UIKit does not try to find your view controller implicitly.
and,
If you choose to use a restoration class, the implementation of your viewControllerWithRestorationIdentifierPath:coder: method should create a new instance of the class, perform some minimal initialization, and return the resulting object.
you will be able to create instance of child view controller with your legacy initWithMOC:
method for the State Preservation and Restoration API to use.
By doing it this way however you loose the benefit of 'it just works and all you need to do is specify the restoration identifier in your storyboard'.
Does anyone know a better way? The obvious way is to retrieve it from app delegate, but that is really a non-preferred way of doing it, so please do not recommend that. Besides all evil on a conceptual level, retrieving it from app delegate kills the benefits of using discardable nested contexts.