1

I'm now reading this great tutorial on which an author explains how to connect view controller to app delegate using .xib file (on section 11) in order to use Core Data in different view controllers. Unfortunately, the post was written in 2011 and quite outdated since the .xib is no more available in Xcode 5 and replaced with storyboard.

With a quick search, I found how to connect AppDelegate to view controller from within storyboard, by using storyboard unique identifier of the view controller on identify inspector pane. However, I wonder whether it is necessary to make such connection in the appdelegate in the current developing environment (Xcode 5 and iOS 7), since I don't have any issues whatsoever even if I don't bother doing such connection - all I did was import the view controller in AppDelegate.h and declared its instance as property (i.e. @property (strong, nonatomic) MyViewController *myViewController;) and finally connect managedObjectContext between the two object, and hence I didn't even declare it as IBOutlet, as explained by the author of the above post.

I've run the simulator with it and I've not encountered any issues so far. However, the "so far" means all that I've written is the connection of the view controller's managedObjectContext to that of App Delegate's, and I wonder whether it has to be connected as IBOutlet from within storyboard to make more convoluted implementation going forward, including insertion and deletion of managed objects or the connection of table view to NSFetchedResultsController.

So could anyone teach me about which way I should take? And if it doesn't have to be connected explicitly, then why was it needed in the older version but not now?

Community
  • 1
  • 1
Blaszard
  • 27,599
  • 40
  • 143
  • 217

1 Answers1

2

The Apple standard XCode template project assigns the managedObjectContext property of the viewController from the AppDelegate as shown in the code below where I have commented out the setting of the property. The issue with this approach is that this method will block the UI if setting up the core data stack takes a long time, for example, when migrating a large store file.

In the v4 sample app the masterViewController has the managedObjectContext set when it is send a notification - and in the meantime it displays a "Loading, please wait..." message. Download and run the app or watch the video.

You can set the managedObjectContext in your viewController any number of ways, as long as you are using the same managedObjectContext when performing operations such as creating, deleting or modifying objects. Take a look at the sample app here to see how I use singleton object to create the Core Data stack and to manage migration of stores to and from iCloud if necessary. Each viewController accesses the singleton's managedObjectContext directly whenever they need to. The detailViewController in the example has a managedObject property and it uses the managedObject's managedObjectContext to handle any updates (saves) so there is no need to directly set a managedObjectContext property.

http://ossh.com.au/design-and-technology/software-development/sample-library-style-ios-core-data-app-with-icloud-integration/

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

    // Override point for customization after application launch.
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
        UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
        splitViewController.delegate = (id)navigationController.topViewController;

        UINavigationController *masterNavigationController = splitViewController.viewControllers[0];
        MasterViewController *controller = (MasterViewController *)masterNavigationController.topViewController;
        //controller.managedObjectContext = [[OSCDStackManager sharedManager] managedObjectContext];
    } else {
        UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
        MasterViewController *controller = (MasterViewController *)navigationController.topViewController;
        //controller.managedObjectContext = [[OSCDStackManager sharedManager] managedObjectContext];
    }
    FLOG(@"didFinishLaunchingWithOptions done.");

    return YES;
}
Duncan Groenewald
  • 6,956
  • 4
  • 31
  • 59
  • So you mean I don't have to connect it from within storyboard - it's enough to import view controller, declare it as property, and connect managed object context of AppDelegate, right? Then I wonder why the author of the above post bother connecting it using xib too, especially since it doesn't resolve the blocking issue as you explained in your answer... – Blaszard Jan 24 '14 at 07:02
  • I don't usually use storyboards at all - but yes, just create a new app using the Xcode master detail template with Core Data and see how they set it in the AppDelegate. No doubt the author did not take into account the blocking issue - neither does Apple's template app. – Duncan Groenewald Jan 24 '14 at 07:20