Reputation: 2976
1. What is the best way to use the same Core Data context from two views?
I have a TabBarController with two ViewController. In the first I want to record a time, which should be stored. In the second there is a TableView showing all records. I did as I was told in the tutorial and initiate all the ManagedDataContext stuff in the App Delegate and pass it to my Controller:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
SecondViewController *tableController = [[SecondViewController alloc] init];
tableController.managedObjectContext = [self managedObjectContext];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:tableController];
[window addSubview: [self.navigationController view]];
[window makeKeyAndVisible];
}
But when I use this variable more than once (also in the same class) i get an (some kind of nullpointer) error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+entityForName: nil is not a legal NSManagedObjectContext parameter searching for entity name 'Event''
While debugging I caught that the managedObjectContext here was null:
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event" inManagedObjectContext:managedObjectContext];
What is the problem here? And what the best solution for the to views?
Is it possible to use a singleton?
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
NSLog(@"managedObjectContext already in use. Returning instance.");
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator: coordinator];
}
NSLog(@"managedObjectContext nil. Returning new instance.");
return managedObjectContext;
}
2. Is it possible to store objects, which differ from the data model?
I need my Class not to store other objects included in mine. Like an array I don't want to save. Is this possible?
Upvotes: 1
Views: 114
Reputation: 3763
1. You are using a tabbarcontroller. The viewControllers on that tab bar need to have property, e.g. managedObjectContext. If you are using a tabbarcontroller via a storyboard or nib you can get at the viewControllers via the viewControllers property on the tabbarcontroller and set the managedObjectContext:
UITabBarController * tabBarController = (UITabBarController *) self.window.rootViewController;
// the first tab is a navcontroller, which contains as a rootVC a custom VC with a property for the MOC
UINavigationController * navController = (UINavigationController *)[tabBarController.viewControllers objectAtIndex:0];
MyViewController1 * rootVCOfNavController = (MyViewController1 *) [[historyNavigationController viewControllers] objectAtIndex: 0];
// the second tab of the tabbarcontroller is a custom VC with a MOC property
MyViewController2 * viewController2 = (MyViewController2 *)[tabBarController.viewControllers objectAtIndex:1];
viewController1 = self.managedObjectContext;
rootVCOfNavController.managedObjectContext = self.managedObjectContext;
This will make sure that both the first 'naked' viewController gets a MOC, and the one that is embedded in a NavigationController. This should take care of the first error. You need to understand your viewController-hierarchy well for this (note: viewController hierarchy is not equal to view-hierarchy).
Using a singleton would in this case be a workaround for a problem you do not fully understand, and therefore ill-advised. There are cases where a singleton would be perfect, but right now your appDelegate can fulfill that role quite good.
2. I am not really clear on what you mean here. Objects that are not defined in the data model can not be stored in core data. You can however save objects in a different model (with its own core data stack). Or persist them some other way. But I guess you are meaning something else. Assuming you mean that you have some managedOjects 'Class' that need not be saved, you can make a secondary MOC, and simply never save it. You can also set it to be a child of the original MOC, so it will fetch all data present in the persistent store of that original mod, but new objects will only be present in the child MOC. Saving a child MOC will move the new objects up into the original MOC and saving that one will persist them to disk. Again, I don't know exactly what you mean with the second question.
Upvotes: 2