Searching with a UISearchbar is slow and blocking the main thread.

Posted by Robert on Stack Overflow See other posts from Stack Overflow or by Robert
Published on 2010-05-13T16:21:49Z Indexed on 2010/05/13 16:24 UTC
Read the original article Hit count: 271

Filed under:
|

I have a Table with over 3000 entries and searching is very slow.

At the moment I am doing just like in the 'TableSearch' example code (but without scopes)

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText: searchString];

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

And the filterContentForSearchText method is as follows:

- (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];
        }
    }
}
}

My question is twofold:

  • How do can I make the searching process faster?
  • How can I stop the search from blocking the main thread? i.e. stop it preventing the user from typing more characters.

For the second part, I tried "performSelector:withObject:afterDelay:" and "cancelPreviousPerformRequests..." without much success. I suspect that I will need to use threading instead, but I do not have much experience with it.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uisearchbar