我有一个UISearchResultsController,它过滤我的NSFetchedResultsController并将过滤后的数据放入数组中。目前,我使用NSPredicate获取搜索栏内容并应用筛选器。以下是我的谓词:
[filteredArray removeAllObjets];
for(Account *account in unfilteredResults){
NSPredicate *predicate;
if(controller.searchBar.selectedScopeButtonIndex == 0){
predicate = [NSPredicate predicateWithFormat:@"accountFirstName BEGINSWITH[cd] %@", searchString];
}else if(controller.searchBar.selectedScopeButtonIndex == 1){
predicate = [NSPredicate predicateWithFormat:@"accountLastName BEGINSWITH[cd] %@", searchString];
}else if(controller.searchBar.selectedScopeButtonIndex == 2){
predicate = [NSPredicate predicateWithFormat:@"group.groupName CONTAINS[cd] %@", searchString];
}
if([predicate evaluateWithObject:account]){
[self.filteredArray addObject:account];
}
}我正在尝试根据名字、姓氏或组名过滤帐户。我知道这些操作很慢,但是怎样才能使它们更快呢?
编辑:
我只是注意到每次迭代都要重新创建谓词,但我仍然认为应该有更好的方法。我确实看到了在Apple视频中做二进制比较的一些事情,但我不知道如何将搜索字符串转换为二进制字符串,更不知道如何获得“下一个最大的”值。
如何用更有效的方法替换BEGINSWITH语句?
发布于 2011-06-29 03:52:39
我假设您的数据对象是CoreData对象,如果是的话,您是否将正在搜索的属性标记为索引?
发布于 2012-04-18 17:23:18
这个职位太旧了,但对别人有帮助。使用此代码,您将只创建一个NSPredicate实例。
NSPredicate *predicate;
if(controller,searchBar,.selectedScopeButtonIndex == 0){
predicate = [NSPredicate predicateWithFormat:@"accountFirstName BEGINSWITH[cd] %@", searchString];
}else if(controller,searchBar,.selectedScopeButtonIndex == 1){
predicate = [NSPredicate predicateWithFormat:@"accountLastName BEGINSWITH[cd] %@", searchString];
}else if(controller,searchBar,.selectedScopeButtonIndex == 2){
predicate = [NSPredicate predicateWithFormat:@"group.groupName CONTAINS[cd] %@", searchString];
}
self.filteredArray = [unfilteredResults filteredArrayUsingPredicate:predicate];https://stackoverflow.com/questions/6515443
复制相似问题