gpt4 book ai didi

ios - 带有核心数据的慢速 UISearchDisplayController

转载 作者:行者123 更新时间:2023-11-29 12:21:19 25 4
gpt4 key购买 nike

我有一个 TableViewController,使用 NSFetchedResultsController 显示核心数据中的大约 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]);
}
}

最佳答案

我最终使用 NSTimer 来延迟 shouldReloadTableForSearchString 方法。 searchTimerPopped 选择器仅在用户停止键入键盘 2 秒时触发。

@property (nonatomic, retain) NSTimer *searchTimer;

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
if (self.searchTimer) {
[self.searchTimer invalidate];
self.searchTimer = nil;
}
self.searchTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(searchTimerPopped:) userInfo:searchString repeats:FALSE];
return NO;
}

- (void)searchTimerPopped:(NSTimer *)timer {
NSString *searchString = [timer userInfo];
ItemSearchScope scopeKey = self.searchDisplayController.searchBar.selectedScopeButtonIndex;
[self searchForText:searchString scope:scopeKey];
[self.searchDisplayController.searchResultsTableView reloadData];
}

关于ios - 带有核心数据的慢速 UISearchDisplayController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30552054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com