Fetching a large data set with Core-Data

I needed loop over a potentially large set of data stored in Core Data, but did not want to force the system to load the entire data set in memory or have Core Data constantly process faults. There seem to be several ways to do this, some easier than others. One robust way would be to use an NSFetchedResultController, but it looked a little more complex than I wanted to deal with ( and involves implementing yet another protocol).

After poking around the NSFetchResult class page for a while I noticed these methods: setFetchLimit and setFetchOffset. It turns out that these are just like the LIMIT and OFFSET keywords in SQL, which is just what I was looking for.

Using this method is just a few extra lines of code. This is what it looks like:
#define kBatchSize 20
// This could take a while.
+ (void) recalculateAllStats {
MyAppDelegate *myAppDelegate = (MyAppDelegate *)
[[UIApplication sharedApplication] delegate];
NSManagedObjectContext *moc =
[myAppDelegate managedObjectContext];
NSEntityDescription *entity =
[NSEntityDescription entityForName:@"Entry"
inManagedObjectContext:moc];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:entity];
// I need to look at all objects, so do not return faults.
[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setFetchLimit:kBatchSize];
[fetchRequest setFetchOffset:0];
// Sort, in case Core Data doesn't enforce select order.
NSSortDescriptor *sorting =
[[NSSortDescriptor alloc]
initWithKey:@"Date" ascending:NO];
[fetchRequest setSortDescriptors:
[NSArray arrayWithObject:sorting]];
// Execute
NSError *error;
NSArray *items =
[moc executeFetchRequest:fetchRequest error:&error];
int i = 0;
int total = 0;
// Continue executing until 0 results are returned.
while ([items count] > 0) {
// Use the data here.
total += [items count];
// Update offset.
i = i + 1;
[fetchRequest setFetchOffset:kBatchSize * i];
items =
[moc executeFetchRequest:fetchRequest error:&error];
}
NSLog(@"Total = %d", total);
[sorting release];
[fetchRequest release];
}

Comments

Popular posts from this blog

OpenSCAD linear extrude that interpolates between two shapes

Taking Stuff Apart: Realistic Modulette-8

XCode4: UITabBarController with UINavigationController using Interface Builder