Database Reference
In-Depth Information
Once we have built the NSFetchRequest , we construct the NSFetchedResultsController .
In its initialization, it accepts an NSFetchRequest , an NSManagedObjectContext , an
NSString for its sectionNameKeyPath , and another NSString for its cacheName . Let's
explore each of these in turn.
NSFetchRequest
The NSFetchRequest retrieves the data from Core Data and makes it available
for use. This is the NSFetchRequest we just defined in code.
NSManagedObjectContext
The NSFetchedResultsController requires an NSManagedObjectContext to perform the
fetch against. This is also the NSManagedObjectContext that the NSFetchedResults -
Controller will be monitoring for changes. Note that the NSFetchedResultsController
is designed to work against user interface elements, and therefore, it works
best when it is pointed at an NSManagedObjectContext that is running on the
main/UI thread. Threading is discussed in more depth in Chapter 5,
Threading , on page 77 .
sectionNameKeyPath
The sectionNameKeyPath is what the NSFetchedResultsController uses to break the
retrieved data into sections. Once the data is retrieved, the NSFetched -
ResultsController calls for a property on each entity using KVC (more on this in
Chapter 8, OS X: Bindings, KVC, and KVO , on page 137 ). The value of that
property will be used to break the data into sections. In our current example,
we have this set to nil , which means our data will not be broken into sections.
However, we could easily add it, as follows:
RecipesV1/PPRecipes/PPRMasterViewController.m
if (fetchedResultsController) return fetchedResultsController;
NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *fetchRequest = nil;
fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@ "Recipe" ];
NSMutableArray *sortArray = [NSMutableArray array];
[sortArray addObject:[[NSSortDescriptor alloc] initWithKey:@ "type"
ascending:YES]];
[sortArray addObject:[[NSSortDescriptor alloc] initWithKey:@ "name"
ascending:YES]];
[fetchRequest setSortDescriptors:sortArray];
NSFetchedResultsController *frc = nil;
frc = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:moc
sectionNameKeyPath:@ "type"
cacheName:@ "Master" ];
 
 
 
Search WWH ::




Custom Search