Database Reference
In-Depth Information
There are many additional examples of how to wire in the NSFetchedResultsController
to the UITableView , but these three highlight the most common usage. Even
with just these three methods, we can see how the NSFetchedResultsController
drastically reduces the amount of code we need to write (and thereby maintain)
to access the data to be displayed.
Listening to the NSFetchedResultsController
In addition to making it very easy for us to retrieve and display the data for
a UITableView , the NSFetchedResultsController also makes it relatively painless to
handle changes in that data. If the values within one of our recipes changes
(perhaps through iCloud, as discussed in Chapter 6, Using iCloud , on page
99 , or through an import), we want our UITableView to immediately reflect those
changes. In addition, if a recipe is removed or added, we want our UITableView
to be accurate. To make sure these updates happen, we need to add the del-
egate methods for the NSFetchedResultsControllerDelegate protocol. As mentioned,
it is common for the UIViewController to also be the delegate for the NSFetched -
ResultsController . There are five methods in this protocol; let's take a look at each
of them.
-controllerWillChangeContent:
The first method, -controllerWillChangeContent: , tells us that changes are about to
start. This method is our opportunity to instruct the UITableView that changes
are coming. Typically this is where we tell the UITableView to stop updating the
user interface so that all of the changes can be displayed at once.
RecipesV1/PPRecipes/PPRMasterViewController.m
- ( void )controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[[self tableView] beginUpdates];
}
-controller: didChangeSection: atIndex: forChangeType:
This method is called when a section changes. The only valid change types
are NSFetchedResultsChangeInsert and NSFetchedResultsChangeDelete . This is our
opportunity to tell the UITableView that a section is being added or removed.
RecipesV1/PPRecipes/PPRMasterViewController.m
- ( void )controller:(NSFetchedResultsController *)controller
didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex
forChangeType:(NSFetchedResultsChangeType)type
{
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:sectionIndex];
switch (type) {
 
 
 
Search WWH ::




Custom Search