我正在从大型数据库中检索一系列对象。每次查找有 1-20 个结果。使用获取请求如下:
NSPredicate *indexPredicate = [NSPredicate predicateWithFormat:
@"index == %@",indexNumber];
我可以在相对较短的时间内得到结果。
CoreData: annotation: total fetch execution time: 0.0623s for 9 rows.
但是,当将所有谓词与 [NSCompoundPredicate orPredicateWithSubpredicates: 合并以在一个操作中进行获取时,速度会下降。
CoreData: annotation: total fetch execution time: 0.3890s for 195 rows.
我尝试过索引、设置提取和批量限制,但无法减少总提取时间。我还尝试使用 GDC 同时一一执行获取请求,但在设置时遇到问题。如何减少获取这些数据的时间?
最佳答案
将 20 个索引查找与 OR
结合起来会在获取过程中进行大量比较。您可以通过将索引值收集到数组中然后使用与 IN
进行单次比较来改善问题。类似的东西
NSArray *indexesToFetch = // A bunch of NSNumbers with index numbers of interest
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"indexNumber in %@", indexesToFetch];
如果您有大量数据需要搜索,这仍然是一项艰巨的工作。但是使用 IN
来完成类似的事情通常比使用一大堆 OR
快得多。
关于ios - 核心数据获取速度慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25253567/