使用Core Data有效显示100,000个项目 [英] efficiently display 100,000 items using Core Data
问题描述
我正在使用NSFetchResultsController在UITableView中显示100,000条以上的记录.可以,但是速度很慢,尤其是在iPad 1上.加载可能需要7秒钟,这对我的用户来说是一种折磨.
I am using a NSFetchResultsController to display 100,000 + records in a UITableView. This works but it is SLOW, especially on an iPad 1. It can take 7 seconds to load which is torture for my users.
我也希望能够使用部分,但这会至少增加3秒钟的工作时间.
I'd also like to be able to use sections but this adds at least another 3 seconds onto the laod time.
这是我的NSFetchResultsController:
Here is my NSFetchResultsController:
- (NSFetchedResultsController *)fetchedResultsController {
if (self.clientsController != nil) {
return self.clientsController;
}
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:self.managedObjectContext];
[request setEntity:entity];
[request setPredicate:[NSPredicate predicateWithFormat:@"ManufacturerID==%@", self.manufacturerID]];
[request setFetchBatchSize:25];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"UDF1" ascending:YES];
NSSortDescriptor *sort2= [[NSSortDescriptor alloc] initWithKey:@"Name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObjects:sort, sort2,nil]];
NSArray *propertiesToFetch = [[NSArray alloc] initWithObjects:@"Name", @"ManufacturerID",@"CustomerNumber",@"City", @"StateProvince",@"PostalCode",@"UDF1",@"UDF2", nil];
[request setPropertiesToFetch:propertiesToFetch];
self.clientsController =
[[NSFetchedResultsController alloc] initWithFetchRequest:request
managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil
cacheName:nil];
return self.clientsController;
}
我在NSPredicate中使用了ManufacturerID的索引.这似乎是一个非常基本的NSFetchRequest-我可以做些什么来加快速度吗?还是我刚刚遇到限制?我一定很想念东西.
I have an index on ManufacturerID which is used in my NSPredicate. This seems like a pretty basic NSFetchRequest - anything I can do to speed this up? Or have I just hit a limitation? I must be missing something.
推荐答案
首先:您可以使用 NSFetchedResultsController
的缓存来加快首次获取后的显示速度.这应该很快下降到几分之一秒.
First: you can use the NSFetchedResultsController
's cache to speed up display after the first fetch. This should quickly go down to a fraction of a second.
第二个:您可以尝试仅显示第一个屏幕,然后在后台获取其余屏幕.我通过以下方式执行此操作:
Second: you can try to display the only the first screenful and then fetch the rest in the background. I do this in the following way:
- 当视图出现时,检查您是否具有首页缓存.
- 如果没有,我将获取第一页.您可以通过设置获取请求的
fetchLimit
来完成此操作.- 如果您使用的是节,请执行两次快速访存以确定第一个节的标题和记录.
- 您可以创建一个子上下文并使用
performBlock:
或 - 使用
dispatch_async()
.
在我最近的一个项目中,该项目的记录超过200K,效果很好.
This worked quite well in one of my recent projects with > 200K records.
这篇关于使用Core Data有效显示100,000个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!