Nektarios
Nektarios

Reputation: 10351

Pre-cache a fetch with Core Data?

I've got an application using Core Data (successfully) and I have an object graph that has a few root nodes that each have a to-many to tens of thousands of children. During my app's run, based on user input, I do the following (pseudocode):

Find the user selected root node,
Then fetch all its children, in ascending alphabetic order based on a property.

The trick here is that this data is constant. My Core Data store is read-only.

Doing this fetch is very time consuming. How can I pre-cache these results so that I completely avoid that fetch and store? The fetched results are used to fill a UITableView. When a user selects a cell of that tableview it drills down in to deeper data in the Core Data store - that data doesn't need to be precached..

Upvotes: 1

Views: 892

Answers (2)

agilityvision
agilityvision

Reputation: 7931

If you are using NSFetchedResultsController with your tableview you can set a cacheName. You can set a batchSize in NSFetchRequest to bring the results in chunks. And also make sure you set the attribute your sorting on as an index in the core data model.

Upvotes: 1

Vincent Bernier
Vincent Bernier

Reputation: 8664

This code come from the Core Data Programming guide (chapter "Core Data Performance ") and shows how to make a prefetch.

NSManagedObjectContext *context = /* get the context */; 
NSEntityDescription *employeeEntity = [NSEntityDescription 
entityForName:@"Employee" inManagedObjectContext:context]; 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
[request setEntity:employeeEntity]; 
[request setRelationshipKeyPathsForPrefetching: 
[NSArray arrayWithObject:@"department"]]; 

Also use a limit on what is retruned from the real fetch. That will help performance.

Upvotes: 0

Related Questions