Multithreaded search with UISearchDisplayController

Posted by Kulpreet on Stack Overflow See other posts from Stack Overflow or by Kulpreet
Published on 2010-06-16T00:15:37Z Indexed on 2010/06/16 0:22 UTC
Read the original article Hit count: 561

I'm sort of new to any sort of multithreading and simply can't seem to get a simple search method working on a background thread properly. Everything seems to be in order with an NSAutoreleasePool and the UI being updated on the main thread. The app doesn't crash and does perform a search in the background but the search results yield the several of the same items several times depending on how fast I type it in. The search works properly without the multithreading (which is commented out), but is very slow because of the large amounts of data I am working with. Here's the code:

- (void)filterContentForSearchText:(NSString*)searchText {
NSAutoreleasePool *apool = [[NSAutoreleasePool alloc] init];
isSearching = YES;

//[self.filteredListContent removeAllObjects]; // First clear the filtered array.

for (Entry *entry in appDelegate.entries)  {
        NSComparisonResult result = [entry.item compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
        if (result == NSOrderedSame)
        {
            [self.filteredListContent addObject:entry];
        }
}

isSearching=NO;
[self.searchDisplayController.searchResultsTableView performSelectorOnMainThread:(@selector(reloadData)) withObject:nil waitUntilDone:NO];
//[self.searchDisplayController.searchResultsTableView reloadData];

[apool drain];  }

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(filteredListContent:) object:searchString];
[self.filteredListContent removeAllObjects]; // First clear the filtered array.
[self performSelectorInBackground:(@selector(filterContentForSearchText:)) withObject:searchString];
//[self filterContentForSearchText:searchString];

// Return YES to cause the search result table view to be reloaded.
return NO; }

© Stack Overflow or respective owner

Related posts about iphone

Related posts about cocoa-touch