ios - 如何在Core Data中的NSFetchedResultsController中使用缓存

标签 ios swift nsfetchedresultscontroller

我正在我的 viewDidLoad 方法中执行此代码

fetchedResultCont = NSFetchedResultsController(fetchRequest: req,
 managedObjectContext: container.viewContext,
 sectionNameKeyPath: nil,
 cacheName: nil)

文档说我们可以使用缓存来加快进程,但没有实现。

谁能告诉我如何使用缓存代替缓存名称中的 nil 来加快进程?

最佳答案

您只需指定一个将用作缓存目录名称的字符串。

fetchedResultCont = NSFetchedResultsController(fetchRequest: req,
     managedObjectContext: container.viewContext,
     sectionNameKeyPath: "SectionKey",
     cacheName: "MyCache")

但是,由于您没有在提取请求中使用部分,因此缓存不会给您带来好处:

来自documentation

The name of the cache file the receiver should use. Pass nil to prevent caching.

Pre-computed section info is cached to a private directory under this name. If Core Data finds a cache stored with this name, it is checked to see if it matches the fetchRequest. If it does, the cache is loaded directly—this avoids the overhead of computing the section and index information. If the cached information doesn’t match the request, the cache is deleted and recomputed when the fetch happens.

关于ios - 如何在Core Data中的NSFetchedResultsController中使用缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56164558/

相关文章:

ios - 编程约束折叠单元格 View

ios - 无法添加 Firebase Invites SDK

swift - 堆栈 View 、Swift 2、iOS 9.3、XCode 7 中的自动约束冲突

swift - RxSwift 可观察数组排序

swift - 使用 NSFetchedResultsController 如何以编程方式设置要显示的 5 个部分以及过滤它们以获取适当的行

ios - 如何使用 coreData 过滤数据

iphone - 'NSData'没有可见的@interface声明选择器

objective-c - 如何使用 CADisplayLink 在 CMRotationMatrix 上应用过滤器

swift - 在315°圆弧上绘制圆角

ios - 保存子上下文时获取 NSFetchedResultsController 的批量更新