Issues handing over managedObjectContext

与世无争的帅哥 提交于 2019-12-25 01:39:16

问题


I have been reading up most questions on this item, but somehow I can't figure this one out.

I managed easily to hand over the ManagedObjectContext from the AppDelegate to the first view in previous applications I tried, where the initial viewController handled also the data.

Now in a new application I am building, I need an initial screen where the user needs to make a selection (through a few possible buttons). On this screen my MOC is still intact as per my troubleshooting (the NSLog message gives me a valid address for the MOC).

Once the selection has been made, the app proceeds to a UIViewController with a TableView in it. In this view I should be able to manage my data, but my MOC returns nil.

The app proceeds to the next view via a UINavigationController.

I incorporated following code in my AppDelegate:

UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
InitialViewController *controller = (InitialViewController *)navigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
return YES;

InitialViewController is the FIRST ViewController, where all is still fine.

The next viewcontroller is called MasterViewController and is the NSFetchedControllerDelegate. Here I can't seem to get access to the NOC. I have tried several approaches and solutions suggested in previous questions on the same topic, but none seem to work.

How can I with proper coding get the NOC to work in the MasterViewController?

Oh yeah, one of the solutions that worked is after the ViewDidLoad in the MasterViewController, I insert the following code:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
self.managedObjectContext = appDelegate.managedObjectContext;

My MOC is available now, but is this the right way to do it?


回答1:


If you are using Storyboards,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    [super prepareForSegue:segue sender:sender];
    if ([[segue identifier] isEqualToString:@"segueNameOfPresentingYourModalViewController"] == YES) {
        UINavigationController *navigationController = segue.destinationViewController;
        YourCustomViewController *aController = (YourCustomViewController *)navigationController.topViewController;
        aController.managedObjectContext = self.managedObjectContext;
    }
    else if ([segue.identifier isEqualToString:@"segueNameOfPushingYourViewController"] == YES) {
        YourCustomViewController *aController = segue.destinationViewController;
        aController.managedObjectContext = self.managedObjectContext;
    }
}

If you are not using Storyboards, you similarly set your view controller's managedObjectContext where you are about to push or present it. Another common place is -tableView:didSelectRowAtIndexPath: method.




回答2:


You write "controller.managedObjectContext" so you clearly added a NSManagedObjectContext* property to InitialViewController. You just need to add the same property to MasterViewController, and assign self.managedObjectContext to it as well.

If you don't want to have to add the moc to each controller, there is nothing wrong with simply declaring a global variable for your MOC:

In your appDelegate.h:

extern NSManagedObjectContext* moc;

Now any .m that has:

#import "AppDelegate.h"

will have access to the moc. You can also just add a property to your appDelegate and then in each controller you can do:

AppDelegate* app = (AppDelegate*)[[UIApplication sharedApplication] delegate];
app.managedObjectContext


来源:https://stackoverflow.com/questions/11838637/issues-handing-over-managedobjectcontext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!