Ok. This is the exact same question as here: Why is NSFetchedResultsController loading all rows when setting a fetch batch size?
But the solution for his doesn't solve for mine.
I have a screen that has several thousand records and is slow to load them all. I set the batch size to 30 (roughly three times the cells on the screen) and it still loops through and loads all the batches for some reason.
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'm not doing anything terribly interesting in this scenario. Here's some of the delegate code (excluding the cell creating, which I confirmed only is called for the number of cells on 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];
}