我遇到的问题如下:
我有一个 UITableView
我从 NSFetchedResultsController
提供数据它从核心数据中检索大约 6000 行。 fetchBatchSize
的NSFetchRequest
设置为 20,如果我不应用任何 NSSortDescriptor
获取速度足够快,不会阻塞 UI 线程。
但是,我确实需要显示按字母顺序排序的那些行,我使用以下 NSSortDescriptor:
[[[NSSortDescriptor alloc] initWithKey:@"optionText" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
这是当事情发生变化时,提取操作现在需要大约 3 秒才能完成,因为 6000 行正在被排序。显然,在那几秒钟内,UI 被阻塞,用户体验很糟糕。
我知道我可以在后台线程中进行提取,然后将对象 ID 传递给主线程,但在这种情况下,我如何仍然使用
NSFetchedResultsController
在主线程中(我也用它来观察数据的变化)?我也有
indexed
我正在排序的属性,但只优化查找而不是排序性能。任何想法将不胜感激,谢谢!
最佳答案
如何使用 NSFetchRequest 的 batchSize
属性(property)?
If you set a non-zero batch size, the collection of objects returned when the fetch is executed is broken into batches. 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.)
关于sorting - 核心数据 : Background fetch, NSFetchedResultsController 和排序时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5661301/