I create a temporary NSManagedObject and associate it with the main NSManagedObjectContext. I need be able to treat it as a fully functioning object (perform fetch requests, etc) inside the context and thus cannot create it without an associated context. I include logic to delete the managed object in ViewWillDisappear under the condition that a new view controller was not just pushed onto the stack:
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
NSArray *viewControllers = self.navigationController.viewControllers;
if (viewControllers.count > 1 && [viewControllers objectAtIndex:viewControllers.count-2] == self) {
// View is disappearing because a new view controller was pushed onto the stack
} else {
// View is disappearing for some other reason
[self.community.managedObjectContext deleteObject:self.community];
}
}
This seems to properly delete the managed object in all cases except for the application quitting. I tried deleting the object in viewDidUnload but it seems the method is not called upon the application quitting. I have considered creating a second managed object context, but want to avoid the major overhead if possible.
Thanks, Graham