I'm writing a test program that use a Tab Controller with multiple tab views. The program downloads multiple XML files, parses and populates sqlite tables through Core Data. The Core Data variables and functions are in the App Delegate file following the Xcode created code.
I started by simply passing the managedObjectContext variable to each subview that needed it, as I initialized it in the App Delegate, e.g.:
FirstViewController *vc1;
vc1 = [[[FirstViewController alloc] initWithNibName:@"FirstView" bundle:nil] autorelease];
[vc1 setManagedObjectContext:self.managedObjectContext];
However, I have a function (resetData) that deletes the entire data store--deletes the persistent store files and sets all the Core Data variables (managedObjectContext, managedObjectModel, persistentStore, etc) to nil, reinitializing everything. This is so that the program can start from scratch and redownload all of the data from the network. When this happens, the subviews now point to the old managedObjectContext.
What's the best way to update the managedObjectContext variable in all the subviews? Manually update the subviews' managedObjectContext variables from the resetData function? Use NSNotificationCenter to send a notification to all views? Is completely deleting and reinitializing all the persistent store files overkill?
I currently have put this getter that just refers back to the App Delegate in all the classes that need to refer to the MOC:
- (NSManagedObjectContext *)managedObjectContext {
MyAppDelegate* ad = (MyAppAppDelegate*)[[UIApplication sharedApplication] delegate];
return [ad managedObjectContext];
}
I'm very new to Cocoa / iOS design patters am trying to figure out the most proper way to do these things! What I've got now works, but I wonder if there are unseen pitfalls or future problems? Thanks!