How to programmatically toggle a UITableView NSFetchedResultsController (or its predicate)?

I have a UITableView that displays a subset of a large number of objects called Documents. A subset is defined by another Selection object. Selected names, an ordered list of documents.

It works fine, except when I want to change the displayed selection at runtime. I get only an empty list.

Basically, I need to change the predicate that is executed by my NSFetchedResultsController so that the new predicate uses a different Selection. I could not get it to work. My last attempt is to completely get rid of NSFetchedResultsController and redistribute it:

- (void) displaySelection:(Selection *)aSet
{
 self.currentSelection = aSet;
 self.fetchedResultsController = nil;
 // methods here don't all use the property but directly the ivar, so we must trigger the getter
 [self fetchedResultsController];
 [self.tableView reloadData];
}

And, of course, the NSFetchedResultsController collector does the right thing:

- (NSFetchedResultsController *)fetchedResultsController
{
    if (fetchedResultsController != nil) { return fetchedResultsController; }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"DocInSelection" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"selection.identifier like %@", currentSelection.identifier];
    [fetchRequest setPredicate:predicate];
<snip>
    [fetchRequest setSortDescriptors:sortDescriptors];
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;
<snip>
    return fetchedResultsController;
}

, . displaySelection:, .

NSFetchedResultsController - UITableView

, NSFetchedResultsController. , NSFetchedResultsController (, , ...). : "" UITableView, NSFetchedResultsController, "switch" ( ) .

, , " " " ", DocInSelection, "ordering" , - .

.

+3
4

, , , OP . . :

- (void) displaySelection:(Selection *)aSet
{
    if (aSet != self.currentSelection) {
        self.currentSelection = aSet;

        NSFetchRequest *fetchRequest = [[self fetchedResultsController] fetchRequest];
        NSPredicate *predicate = nil;
        NSEntityDescription *entity = nil;
        entity = [NSEntityDescription entityForName:@"DocInSelection" inManagedObjectContext:managedObjectContext];
        predicate = [NSPredicate predicateWithFormat:@"selection.identifier like %@", currentSelection.identifier];
        [fetchRequest setEntity:entity];
        [fetchRequest setPredicate:predicate];

        [NSFetchedResultsController deleteCacheWithName:@"Root"];

        NSError *error = nil;
        if (![[self fetchedResultsController] performFetch:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            abort();
        }       
    }
    [self.tableView reloadData];
}
+6

NSFetchedResultsController (FRC) , .

FRC , tableview controller fetchedResultController FRC, . , . FRC, .

, endUpdates, , tableview beginUpdates. , , FRC . reloadData.

+15

iOS, :

: . , .

: NSFetchedResultsController

iOS 3.2.

.

+3

Important note: if you “overwrite” the fetchController object, first clear it .delegate, otherwise you will get crashes when deleting lines, etc., when the old fetchController and its delegate receive events.

0
source

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


All Articles