我有一个包含8000多个条目的大型数据库。我想在tableview
中加载并列出。我用了NSFetchedResultsController
。除了糟糕的表现之外,它的工作正常。我找到了教程here
并在performFetch:
方法中添加了viewDidLoad:
方法。但是加载视图需要2-3秒。
这是我的代码。
- (void)loadFetchResultsController {
NSError *error;
if (![[self fetchedResultsController] performFetch:&error]) {
// Update to handle the error appropriately.
NSLog(@"Error occurs while fetching the foods with fetch results controller %@, %@", error, [error userInfo]);
exit(-1);
}
}
- (NSFetchedResultsController *)fetchedResultsController
{
if (isSearching)
{
[NSFetchedResultsController deleteCacheWithName:@"SearchResults"];
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Food" inManagedObjectContext:_managedObjectContext];
NSPredicate *hiddenPredicate = [NSPredicate predicateWithFormat:@"hidden == %@",[NSNumber numberWithBool:NO]];
if (isSearching)
{
hiddenPredicate = [NSPredicate predicateWithFormat:@"(hidden == %@) AND (name contains[cd] %@ OR foodDescription contains[cd] %@)",[NSNumber numberWithBool:NO], searchTerm,searchTerm];
}
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:hiddenPredicate];
NSSortDescriptor *sort = [[NSSortDescriptor alloc]
initWithKey:@"name" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sort]];
[fetchRequest setFetchBatchSize:20];
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:_managedObjectContext sectionNameKeyPath:@"name.stringGroupByFirstInitial" cacheName:(isSearching) ? @"SearchResults" : @"Root"];
_fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
我使用了这些工具,发现调用performFetch:方法会在viewDidLoad:
调用时第一次加载整个数据。
我的food
数据模型是这样的:
任何提高性能和摆脱延迟的想法..任何帮助都将不胜感激。
答案 0 :(得分:0)
我认为您需要为获取请求设置提取限制,而不仅仅是批量大小。尝试调用[fetchRequest setFetchLimit:20];看看是否有帮助。这应该导致获取前20个记录。然后,当用户滚动浏览您的数据时,请进行其他获取请求以获取更多数据。