NSFetchedResultsController loads all rows, even if I have a batch set

Ok This is the same question as here: Why does NSFetchedResultsController load all rows when setting the batch size?

But the solution for him does not solve for me.

I have a screen with several thousand entries and it slowly loads them all. I set the batch size to 30 (about three times as many cells on the screen), and for some reason, it still goes through and loads all the batches.

Here is the code

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

    // SELECT * from GuestCard
    NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"GuestCard" inManagedObjectContext:self.context];
    [fetchRequest setEntity:entity];
    // ORDER BY updated DESC
    NSSortDescriptor* updatedSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"created" ascending:NO];
    [fetchRequest setSortDescriptors:@[updatedSortDescriptor]];
    fetchRequest.fetchBatchSize = 30;
    NSString *cacheName = self.isReportProblemView ? @"reportProblemGuestCardsAll" : @"guestCardsAll";

    [NSFetchedResultsController deleteCacheWithName:cacheName];
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.context sectionNameKeyPath:@"sectionIdentifier" cacheName:cacheName];
    aFetchedResultsController.delegate = self;
    self.guestCardFetchedResultsController = aFetchedResultsController;

    // Clean up

    NSError *error = nil;
    if (![[self guestCardFetchedResultsController] performFetch:&error]) {
    }

    return self.guestCardFetchedResultsController;
}

I am not doing anything terrible in this scenario. Here's some delegate code (except for creating a cell, which I confirmed only, is called for the number of cells on the screen):

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if ([self.guestCardFetchedResultsController.fetchedObjects count] == 0) {
        return 1;
    }
    // Return the number of sections.
    return [[self.guestCardFetchedResultsController sections] count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self.guestCardFetchedResultsController.fetchedObjects count] == 0) {
        return 1;
    }
    // Return the number of rows in the section.
    id <NSFetchedResultsSectionInfo> sectionInfo = [guestCardFetchedResultsController sections][section];
    return [sectionInfo numberOfObjects];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if ([self.guestCardFetchedResultsController.fetchedObjects count] == 0) {
        return @"";
    }

    return [[self.guestCardFetchedResultsController sections][section] name];
}
+4
3

docs NSFetchedResultsController, , , (), , , (sectionIdentifier). , , :

fetchRequest,

, sectionIdentifier, . , .

 NSSortDescriptor* updatedSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"created" ascending:NO];
 NSSortDescriptor* sectionSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"sectionIdentifier" ascending:NO];
 // It critical the sectionSortDescriptor is first
 [fetchRequest setSortDescriptors:@[sectionSortDescriptor, updatedSortDescriptor]];

: created, sectionIdentifier - , , , Core Data , /, .

+5

fetchBatchSize, NSArray, . , , . , ( ) , , fetchBatchSize ( , -, 4). - run:

-com.apple.CoreData.SQLDebug 1

. , .

, , , .

- sectionIdentifier , , , , . , ToFetch sectionIdentifier, , , , , .

+2

, , , .

fetchBatchSize, fetchRequest , . , . [fetchResultController objectAtIndexPath:indexPath], ManagedObjectContext ( ), fetchRequest.

, fetchBatchSize 0, , , . , fetchLimit fetchOffset , . .

-1

All Articles