简体   繁体   中英

NSFetchedResultsController refresh UITableView entries without reload stutter

I have a UITableView driven by a NSFetchedResultsController. I call a method to create a NSPredicate that filters my entities that have a timestamp since today. And in my NSFetchedResultsController I apply the predicate.

if (appDelegate.displayTodayOnly) {
    [fetchRequest setPredicate:datePredicate];
}

This all works great. However, occasionally I need to view the entries previous to 'today.' I do this by swiping down on the table view when it is already at the beginning of the table.

-(void) scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {  
    if (self.tableView.contentOffset.y<0) {
        if (appDelegate.displayTodayOnly) {
            [appDelegate setDisplayTodayOnly:NO];
            NSInteger resultsOffset = [[self.fetchedResultsController fetchedObjects] count];
            //[self.tableView beginUpdates];
            [__fetchedResultsController release];
            __fetchedResultsController=nil;
            NSError *error;
            if (![[self fetchedResultsController] performFetch:&error]) {
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                exit(-1);  // Fail
            }
            //[self.tableView endUpdates];
            NSInteger fullCount = [[self.fetchedResultsController fetchedObjects] count];
            if (fullCount>resultsOffset) {
                NSUInteger sect = [[self.fetchedResultsController sections] count]-2;
                id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:sect];
                NSUInteger rows = [sectionInfo numberOfObjects]-1;
                NSIndexPath *indexing = [NSIndexPath indexPathForRow:rows inSection:sect];
                NSLog(@"IndexPath = section:%d row:%d",sect,rows);
                [self.tableView reloadData];
                [self performSelector:@selector(scrollToIndexPath:) withObject:indexing afterDelay:0.0001];
            }
        }
    }
}

This works also, but is inelegant. It discards the fetchedResultsController, and reloads the table starting with the entries from the beginning of time as far as the entries are concerned. Then, I have it scroll to the index path above the entry of the previous table.

What I would like to happen is for the entries to load and insert before the top of the current table. Does anyone know of an elegant way to change the fetch for that to happen?

What I usually do is to set

self.fetchedResultsController = nil; 

and reload. It is very efficient and results in minimal code (assuming you load and create the fetched results controller lazily as in the Xcode templates).

I'm posting my solution in hopes that it may help someone else in need.

I'm still setting the NSFetchedResultsController instance to nil, and getting a new one with a different predicate. Then, instead of reloading the table (which caused the disorienting jump), in a for(loop) I manually call [self.tableView insertSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationNone]; which comes almost directly out of the boilerplate controllerWillChangeContent: of <NSFetchedResultsControllerDelegate> method. This works because the last section is always going to be the section for today. If there aren't any entries for today, I need to reloadData because there will be an inconsistency of the number of sections that are inserted because today's empty section counts as a section.

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{   
    if (self.tableView.contentOffset.y<0) {
        if (appDelegate.displayTodayOnly) {
            [appDelegate setTodayOnlyMemory:NO];
            NSUInteger resultsOffset = [[self.fetchedResultsController fetchedObjects] count];
           [__fetchedResultsController release];
            __fetchedResultsController = nil;
            self.fetchedResultsController = nil;

            NSError *error = nil;
            if (![self.fetchedResultsController performFetch:&error])
            {
                NSLog(@"error in fetched results controller of Memory Notes");
                NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
                abort();
            }
            if (resultsOffset) {
                [self.tableView beginUpdates];
                NSInteger afCount = ([[self.fetchedResultsController sections] count]-1);
                for (NSInteger i=0; i<afCount; i++) {
                    [self.tableView insertSections:[NSIndexSet indexSetWithIndex:i] withRowAnimation:UITableViewRowAnimationNone]; 
                }
                [self.tableView endUpdates];
            } else {
                [self.tableView reloadData];
            }            
            [self performSelector:@selector(scrollToSpot) withObject:nil afterDelay:0.01f];
        }
    }
}

Additionally, here is my scrollToSpot method for ending up at the right spot.

-(void)scrollToSpot
{    
    NSArray *sectArray = [self.fetchedResultsController sections];
    if ([sectArray count]>1) {
        id <NSFetchedResultsSectionInfo> sectionInfo = [sectArray objectAtIndex:([sectArray count]-2)];
        NSInteger theRow = [sectionInfo numberOfObjects]-1;
        NSIndexPath* ipath = [NSIndexPath indexPathForRow:theRow inSection:([sectArray count]-2)];
        NSLog(@"Row:%d",ipath.row);
        [self.tableView scrollToRowAtIndexPath:ipath atScrollPosition:UITableViewScrollPositionTop animated:NO];
        NSLog(@"ContentOffset:%.0f",self.tableView.contentOffset.y);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM