I’m executing a fetch request that returns about 2000 entities. Currently, this takes about 20 seconds on my device. So I thought I might set a fetch limit of 100, and then when the user scrolls to the end of the table view, fetch the next 100 entities. This can be accomplished using NSFetchRequest's setFetchLimit and setFetchOffset.
However, what I can’t figure out is, if on my second fetch where I’m fetching objects 101-200, what would happen to the objects 1-100? Would I have to use separate NSFetchedResultsController for every 100 items, and then configure my table view data source methods to query based on multiple fetch results controllers? Or can I somehow use the same NSFetchedResultsController to somehow fetch 100 entities at a time, but upon every subsequent fetch, just add the next 100 items to the original 100 items?
Edit: Here’s my code:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"MessageObject" inManagedObjectContext:appDelegate.managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate= [NSPredicate predicateWithFormat:@"ANY tags.tagName==%@", currentTagObject.tagName];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sort= [[NSSortDescriptor alloc] initWithKey:@"createDate" ascending:NO selector:@selector(compare:)];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:5];
You don’t actually need to do anything to achieve the fetching in batches of the size you’ve specified. You will get the behavior you’re describing simply be executing a single fetch request on a single controller – by setting the batch size property, you’re simply hinting to CoreData what you consider to be the optimal batch size by your own consideration. CoreData will handle fetching objects in sequential batches of the size specified as you need them – and it will fault managed objects when necessary for memory consumption.