This question is in reference to an iPhone app which uses Xcode’s Master Detail template with Core Data. The app supports iOS 5.1 and above.
I’m using NSFetchedResultsController to manage the items in a UITableView which is the second view controller in my UINavigationController stack. The user selects from the table view and the third view controller in the stack is pushed onto the view. Pretty standard stuff, and I’m enjoying the much simplified access to Core Data objects afforded by the NSFetchedResultsController.
Once in the third view controller, the user is looking at a set of data selected from the list in the table view. I have added a couple of buttons to this view for navigating to the previous or next set of data. Think of the iOS Mail app and the up/down buttons you see when looking at an email. You can use these buttons to select the next or previous email in the list, without navigating back to the list (table view) of emails.
I tried to find the simplest method for identifying which data should be loaded from the data store when one of these next/previous buttons is tapped. As far as I can tell, this is to use the fetched results controller from the previous view controller in the stack. I can simply ask Core Data to return the results for the next or previous index path in the table view.
While in my third (Detail) view controller I’m able to get access to the second (Master) view controller’s fetched results controller as follows:
NSFetchedResultsController *fetch = [[self.navigationController.viewControllers objectAtIndex:1] fetchedResultsController];
NSIndexPath *indexPath = [fetch indexPathForObject:self.detailItem];
NSLog(@"Section: %d, Row: %d", indexPath.section, indexPath.row);
Before I go any further, I need to know that view controller and its fetched results controller will always be around. As I understand it, it will be retained until there is a shortage of memory, at which point it could be released. My detail views can consume a lot of memory when displaying their data, and will consume more and more as the app is used to gather more data over time.
Can I be sure these items (previous view controller in the stack and its fetched results controller) will stay in memory while my user is interacting with the detail view controller?
If not, is there a way for me to ensure they are retained?
Otherwise, is there a simpler or more elegant way for me to achieve the aim?
Your second view controller will never be released. Also not during low memory situations. What can happen is that
viewDidUnload(deprecated and no longer called in iOS 6) is called in low memory situations. So as long as you don’t get rid of yourNSFetchedResultsControllerin that method you should be fine.