I have an app I am working on. There is a navigation controller, and my appDelegate is passing its managedObjectContext to the nav controllers root controller like so:
RootViewController *rootViewController = (RootViewController *)[navigationController topViewController];
rootViewController.managedObjectContext = self.managedObjectContext;
(Bonus question – I’ve read the above is the preferred way to pass the context, as opposed to some examples I see where the view controller grabs the context from the delegate – is this correct? Also, do I need to release rootViewController above, or am I correct in that it is autoreleased because it was not created using alloc or new?)
Anyway, the main view of the app has a button – when clicked, it records a timestamp and saves it in core data. The app then displays a second view, which is where the user is going to work with a subset of the data. This second view allows the user to choose to see all timestamps from the current week, month, or year.
My initial thought is to pass the managedObjectContext from the rootVC to the detailVC, and perform the data read and queries in the detailVC. Is this the proper way to go about this, or is it better to perform the query at the rootVC and pass the data to the detail controller as an array or something? Or does it make no difference other than organizationally (no performance difference) – 6 of one, half a dozen of the other?
You don’t really have to worry about the efficiency of this, it will perform fine whichever way you do it – you’re just passing a pointer around. That’s not to say this is a decent way of going about things though.
Apple’s Core Data templates are pretty terrible. Generally speaking, it’s better to factor out your data management into a separate manager class that has responsibility for managing your data.