How to switch UITableView's NSFetchedResultsController (or its predicate) programmatically?

前端 未结 4 874
故里飘歌
故里飘歌 2020-12-08 17:43

I have a UITableView that displays a subset of a large number of entities named \"Documents\". The subset is defined by another entity \"Selection\". Selections are named, o

4条回答
  •  既然无缘
    2020-12-08 18:41

    After I posted my question, I tried a variant of the code the OP of the other question showed. It works for me. Here it is:

    - (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];
    }
    

提交回复
热议问题