NSFetchedResultsController / Parent-Child

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!

+4
source share
1 answer

You need NSPredicate to select ListItem , where selectedList matches the list property.

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"list == %@", self.selectedList]; [fetchRequest setPredicate:predicate]; 

Here you need to know that the predicate will change depending on the selectedList , which means that the query query will change. If you change the select query, Apple has this warning in the NSFetchedResultsController Reference .

You cannot just change a query to change results. If you want to change the query for the selection, you must:

  • If you use a cache, delete it (using +deleteCacheWithName: .

  • Normally, you should not use the cache if you change the query to fetch.

  • Modify the fetch request. Call -performFetch:

+1
source

Source: https://habr.com/ru/post/1387102/


All Articles