我有一个表超过3000个条目和搜索是非常缓慢的。使用UISearchbar搜索很慢,并阻止主线程
在我做就像在“TableSearch”示例代码(但没有范围)
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
[self filterContentForSearchText: searchString];
// Return YES to cause the search result table view to be reloaded.
return YES;
}
而且filterContentForSearchText方法的时刻如下:
- (void) filterContentForSearchText:(NSString*)searchText
{
// Update the filtered array based on the search text
// First clear the filtered array.
[filteredListContent removeAllObjects];
// Search the main list whose name matches searchText
// add items that match to the filtered array.
if (fetchedResultsController.fetchedObjects)
{
for (id object in fetchedResultsController.fetchedObjects)
{
NSString* searchTarget = [tableTypeDelegate getStringForSearchFilteringFromObject:object];
if ([searchTarget rangeOfString:searchText
options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)].location != NSNotFound)
{
[filteredListContent addObject:object];
}
}
}
}
我的问题是双重的:
- 如何使搜索过程更快?
- 如何阻止搜索阻止主线程?即阻止它阻止用户输入更多字符。
对于第二部分,我尝试了“performSelector:withObject:afterDelay:”和“cancelPreviousPerformRequests ...”,但没有取得太大的成功。我怀疑我需要使用线程,但我没有太多的经验。
是的,这是一个很好的建议。 – Robert 2011-03-10 20:26:44