使用Core Data预先缓存提取?

时间:2012-01-09 20:26:00

标签: core-data

我有一个使用Core Data的应用程序(成功),我有一个对象图,它有一些根节点,每个节点有一个到多个到几万个孩子。在我的应用程序运行期间,根据用户输入,我执行以下操作(伪代码):

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

这里的技巧是这个数据常量。我的核心数据存储是只读的。

执行此提取非常耗时。如何预先缓存这些结果,以便完全避免获取和存储?获取的结果用于填充UITableView。当用户选择该表视图的单元格时,它会深入挖掘核心数据存储中的更深层数据 - 不需要预先缓存该数据。

2 个答案:

答案 0 :(得分:1)

如果您在桌面视图中使用NSFetchedResultsController,则可以设置cacheName。您可以在NSFetchRequest中设置batchSize以将结果打包为块。并且还要确保将排序属性设置为核心数据模型中的索引。

答案 1 :(得分:0)

此代码来自Core Data Programming指南(“核心数据性能”一章) “)并显示如何进行预取。

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"]]; 

还对实际提取中重新生成的内容使用限制。这将有助于提高绩效。