Skip managedObjectContext (master data) for other classes, performed correctly?

I used the default template provided by Apple with the underlying data (managedObjectContext is in AppDelegate). At first I included appdelegate.h in every class that I need to use managedObjectContext, but I saw that this is not the right way to do this. Apple says it's best to pass context to other classes that need it, and so on, so I ended it up like this. The fact is that it looks a bit β€œhacky” as I did, and I wonder if there is a better option or my decision is correct.

Currently, my application is configured like this (here is the SS of my storyboard): enter image description here

So my root window is a UITabBarController, and each tab is a UINavigationController, which points to several UITableViewController / UIViewController.

Here is what I have in my Appdelegate to pass the managedObjectContext instance to 2 tabs:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { UITabBarController *rootViewController; UINavigationController *navigationController; ItemsTableViewController *itemsTableViewController; // Get the root window (UITabBarController) rootViewController = (UITabBarController *)self.window.rootViewController; // Get the second item of the UITabBarController navigationController = [[rootViewController viewControllers] objectAtIndex:1]; // Get the first item of the UINavigationController (ItemsTableViewController) itemsTableViewController = [[navigationController viewControllers] objectAtIndex:0]; itemsTableViewController.managedObjectContext = self.managedObjectContext; // Get the third item of the UITabBarController (again ItemsTableViewController) navigationController = [[rootViewController viewControllers] objectAtIndex:2]; // Get the first item of the UINavigationController (ItemsTableViewController) itemsTableViewController = [[navigationController viewControllers] objectAtIndex:0]; itemsTableViewController.managedObjectContext = self.managedObjectContext; return YES; } 

Everything works well, but you need to call objectAtIndex several times to get the right ViewController looks meh ...

Anyone like a better solution?

Thanks!

+4
source share
1 answer

You should learn the prepareForSegue: method to pass the managed ObjectOnContext to other controllers.

Alternatively, you can subclass the tab bar controller and add a managed object context as a property that you can access from anywhere in your application if you also have a tab bar controller.

Finally, if you are only going to use one context (i.e. not many threads), you can always set up the CoreDataHelper class using the class method, which returns your default context whenever you request it. To avoid importing a helper in each individual class, simply add a helper to your precompiled header file (.pch) and let it also import the <CoreData/CoreData.h> framework.

If you want to see an example of how to do this, check out MagicalRecord on github https://github.com/magicalpanda/MagicalRecord

[EDIT] Here is an example of how to pass context using the prepareForSegue method. Remember that this method is called when segue initialization begins, and it gives you the ability to configure a view controller that is about to be pressed. Here you can pass delegate links and assign values ​​to other variables in the destination view controller:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { NSString *segueIdentifier = [segue identifier]; if ([segueIdentifier isEqualToString:@"YourSegueIdentifier"]) // This can be defined via Interface Builder { MyCustomViewController *vc = [segue destinationViewController]; vc.managedObjectContext = self.managedObjectContext; } } 
+10
source

Source: https://habr.com/ru/post/1391359/


All Articles