NSFetchedResultsController is set to 5 for fetch batch size. We notice it's slow loading, so we enable SQLDebug. For some reason, it's loading 5 rows at a time, but it's doing this for all 100 rows on initial load. Once it's loaded, it seems to load 5 rows at a time.
When watching the console, I see the query ran 20 times; there are a total of 100 rows. I would have expected this to run 1 time with the first 5 rows and as you scroll, more rows would be loaded. I also tried changing the fetch size and it has the same behavior.
The setup of the fetched results controller is below.
- (NSFetchedResultsController*) fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
[NSFetchedResultsController deleteCacheWithName:@"Root"];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Message" inManagedObjectContext:_context];
fetchRequest.predicate = [NSPredicate predicateWithFormat:@"conversation = %@", conversation];
[fetchRequest setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"timestamp" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:5];
NSFetchedResultsController *theFetchedResultsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:_context sectionNameKeyPath:nil
cacheName:@"Root"];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
[sort release];
[fetchRequest release];
[theFetchedResultsController release];
return _fetchedResultsController;
}