【发布时间】:2015-05-30 22:38:57
【问题描述】:
我有一个 TableViewController 使用 NSFetchedResultsController 显示来自 Core Data 的 40 000 行。
我使用 UISearchDisplayController 实现了实时搜索(支持 IOS 7)。 它可以工作,但是搜索时在键盘上输入很慢......
如果有人能指出正确的方向并告诉我哪里可能出错,我将不胜感激。
这是我的 TableViewController 中的 UISearchResultsUpdating 部分
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
ItemSearchScope scopeKey = controller.searchBar.selectedScopeButtonIndex;
[self searchForText:searchString scope:scopeKey];
return YES;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
NSString *searchString = controller.searchBar.text;
[self searchForText:searchString scope:searchOption];
return YES;
}
- (void)searchForText:(NSString *)searchText scope:(ItemSearchScope)scopeOption
{
if (self.managedObjectContext)
{
NSString *predicateFormat = @"%K CONTAINS[cd] %@";
NSString *searchAttribute1 = @"attribute1";
NSString *searchAttribute2 = @"attribute2";
NSString *searchAttribute3 = @"attribute3";
if (scopeOption == searchScopeDebut) {
predicateFormat = @"%K BEGINSWITH[cd] %@";
}
if (scopeOption == searchScopeFin) {
predicateFormat = @"%K ENDSWITH[cd] %@";
}
NSPredicate *p1 = [NSPredicate predicateWithFormat:predicateFormat, searchAttribute1, searchText];
NSPredicate *p2 = [NSPredicate predicateWithFormat:predicateFormat, searchAttribute2, searchText];
NSPredicate *p3 = [NSPredicate predicateWithFormat:predicateFormat, searchAttribute3, searchText];
NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[p1, p2, p3]];
[self.searchFetchRequest setPredicate:predicate];
NSError *error = nil;
self.filteredList = [self.managedObjectContext executeFetchRequest:self.searchFetchRequest error:&error];
if (error)
{
NSLog(@"searchFetchRequest failed: %@",[error localizedDescription]);
}
}
}
【问题讨论】:
-
您的提取请求代码可能存在问题。如果 Core Data 尝试获取数千行以匹配搜索查询,它将非常慢。确保设置了 fetch 请求的“batchSize”和/或“fetchLimit”属性以加快它的速度……但一定要确保设置了“batchSize”
-
我尝试在我的 fetchedResultsController 方法中添加“ [fetchRequest setFetchBatchSize:20]; ”,但它不会影响 UISearchDisplayController 的性能不佳
标签: ios iphone core-data nsfetchedresultscontroller uisearchdisplaycontroller