我對任何類型的多線程都很陌生,而且根本看不到一個簡單的搜索方法正確地處理後臺線程。一切似乎都是按照NSAutoreleasePool的順序進行的,UI在主線程上進行更新。該應用程序不會崩潰,並在後臺執行搜索,但搜索結果會多次生成幾個相同的項目,具體取決於我輸入的速度。搜索正常工作時沒有多線程(已被註釋掉),但由於我正在處理大量的數據,所以速度很慢。下面的代碼:iPhone多線程搜索
- (void)filterContentForSearchText:(NSString*)searchText {
isSearching = YES;
NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
/*
Update the filtered array based on the search text and scope.
*/
//[self.filteredListContent removeAllObjects]; // First clear the filtered array.
for (Entry *entry in appDelegate.entries)
{
NSComparisonResult result = [entry.gurmukhiEntry compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
if (result == NSOrderedSame)
{
[self.filteredListContent addObject:entry];
}
}
[self.searchDisplayController.searchResultsTableView performSelectorOnMainThread:(@selector(reloadData)) withObject:nil waitUntilDone:NO];
//[self.searchDisplayController.searchResultsTableView reloadData];
[apool drain];
isSearching = NO; }
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
if (!isSearching) {
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
[self performSelectorInBackground:(@selector(filterContentForSearchText:)) withObject:searchString];
}
//[self filterContentForSearchText:searchString];
return NO; // Return YES to cause the search result table view to be reloaded. }