Recipes for modifying a fetch request for NSFetchedResultsController and reloading table data

From apple doc Changing a query to a selection I see what can be changed NSFetchRequestfor NSFetchedResultsController. The steps are easily customizable.

After the call, performFetch:I think it is necessary to call reloadDatain the table view. How to make such a call?

Reading some stackoverflow topics, I saw that calling this method should work in most cases. But is there a proper way to do this?

Q How to switch the UITableView NSFetchedResultsController (or its predicate) programmatically? , TechZen wrote that:

Just make sure you send the tableview beginUpdates swap controllers yourself, and then endUpdates when you are done. This prevents the table from requesting data in a narrow window when the FRC is unloaded. Then call reloadData.

Could you explain what this means?

+3
source share
1 answer

Assuming the correct fetching logic (some kind of conditional statement) is in the receiver of your NSFetchedResultsController instance. Then it's really easy

self.fetchedResultsController = nil; // this destroys the old one
[self.tableview reloadData]; 
// when the table view is reloaded the fetchedResultsController will be lazily recreated

: , . NSDictionary entityDescription, NSFetchedResultsController. fetchRequest, entityDescription, setter reset fetchedResultsController . .

- (NSFetchedResultsController *)fetchedResultsController 
{
    if (__fetchedResultsController != nil) {
        return __fetchedResultsController;
    }
    if (self.entityDescription == nil) {
        return nil;
    }
    // Set up the fetched results controller.
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:[self.entityDescription objectForKey:kEntityName]];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.
    if ([[self.entityDescription objectForKey:kEntitySortField] isEqualToString:@"null"] == NO) {
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:[self.entityDescription objectForKey:kEntitySortField] ascending:YES];
        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.moc sectionNameKeyPath:nil cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

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

    return __fetchedResultsController;
}

- (void)setEntityDescription:(NSDictionary *)entityDescription
{
    _entityDescription = entityDescription;
    self.fetchedResultsController = nil;
    [self.tableView reloadData];
}
+8

All Articles