I am working on my first Core Data-enabled application, and itβs hard for me to figure out how to properly configure NSFetchedResultsController. I have two objects:
/-----------\ /-----------\ | List | | ListItem | | --------- | | --------- | | listName | | itemName | | --------- | | --------- | | listItems |<---->> | list | \ --------- / \ --------- /
I have two kinds (each inherits from the UITableViewController and each of them is supported by NSFetchedResultsController ). The first view is a UITableView that displays all List items. When a row is selected, it advances a second view.
I have a property in the view controller called @property (strong, nonatomic) List *selectedList that I assign with the selected List before I click the view.
I had a problem with setting up the second view with only displaying ListItem objects, the parent - the selected List . I know this belongs somewhere in the NSFetchedResultsController , but I don't know where.
Here is my code:
- (NSFetchedResultsController *)fetchedResultsController { if (__fetchedResultsController != nil) { return __fetchedResultsController; } // Set up the fetched results controller. // Create the fetch request for the entity. NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; // Edit the entity name as appropriate. NSEntityDescription *entity = [NSEntityDescription entityForName:@"ListItem" inManagedObjectContext:self.managedObjectContext]; [fetchRequest setEntity:entity]; // Set the batch size to a suitable number. [fetchRequest setFetchBatchSize:20]; // Edit the sort key as appropriate. NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timeStamp" ascending:NO]; NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil]; [fetchRequest setSortDescriptors:sortDescriptors]; // Edit the section name key path and cache name if appropriate. // nil for section name key path means "no sections". NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"Detail"]; aFetchedResultsController.delegate = self; self.fetchedResultsController = aFetchedResultsController; NSError *error = nil; if (![self.fetchedResultsController performFetch:&error]) { NSLog(@"Unresolved error %@, %@", error, [error userInfo]); abort(); } return __fetchedResultsController; }
Do I need to add some sort descriptor, or the answer lies with something else? Thanks!
source share