我正处于优化我的核心数据代码阶段,
我正在制作一个消息传递应用程序,我只想一次加载 50 条消息。 但是,查看下面的时间配置文件数据,fetchrequest 一次加载所有消息。之后,它甚至不滚动就以 50 条为一组加载所有消息。所以就好像什么都没做一样。
如有任何帮助,我们将不胜感激。
lazy var fetchedResultsControler: NSFetchedResultsController = {
let fetchRequest = NSFetchRequest(entityName: "Mesages")
fetchRequest.fetchBatchSize = 20
fetchRequest.sortDescriptors = [NSSortDescriptor(key: "timestamp", ascending: true)]
fetchRequest.predicate = NSPredicate(format: "user.id = %@", self.friend!.id!)
let moc = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let frc = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: moc, sectionNameKeyPath: nil, cacheName: nil)
frc.delegate = self
return frc
}()
override func viewDidLoad() {
super.viewDidLoad()
do {
try fetchedResultsControler.performFetch()
} catch let err {
print(err)
}
self.fetchData = fetchedResultsControler.fetchedObjects as! [Mesages]}
这是 CoreData 时间分析数据:
最佳答案
这是正确的行为,根据 NSFetchRequest
documentation :
When the fetch is executed, the entire request is evaluated and the identities of all matching objects recorded, but no more than batchSize objects’ data will be fetched from the persistent store at a time. The array returned from executing the request will be a proxy object that transparently faults batches on demand. (In database terms, this is an in-memory cursor.
相反,如果您想要显式控制提取分组,请使用 fetchLimit
和 fetchBatchSize
的组合。但此时你正在与 NSFetchedResultsController
的特性作斗争。
您真正想用 fetchData
数组做什么?
关于ios - NSFetchedResultsController 一次加载所有消息,获取批量大小没有修复它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38962484/