Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Search / filtering performance on iPhone

Hi all,

I am trying to implement search / filtering on my iPhone application. As I receive textDidChange messages, I search through my list and update the search controller with a new list of all the matches. The problem that I have is that the search through the list is taking 0.67 seconds to complete (3,300 items in the list) and the typing lags on the display. I'm concerned because I have another 1,700 or so items to add to the list before it is complete.

Is there some way of optimizing this?

Here's my current code:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

if([searchText localizedCaseInsensitiveCompare:@""] != NSOrderedSame){
if ([searchText length] < lastSearchLength) {
[self loadSearchArray];
}

NSMutableArray *removeList = [[NSMutableArray alloc] init];
for (Gobo *gobo in searchList)
{
NSRange range1 =[(NSString *)[gobo name] rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSRange range2 =[[NSString stringWithFormat:@"%d",[gobo number]] rangeOfString:searchText options:NSCaseInsensitiveSearch];

if( (range1.location == NSNotFound) && (range2.location == NSNotFound) ){
[removeList addObject:gobo];
}
}
[searchList removeObjectsInArray:removeList];
[removeList release];
} else {
// Else is called when the UISearchBar is empty
[self loadSearchArray];
}

// Called to refresh the table view with changed data provider.
[_searchViewController refresh];

lastSearchLength = [searchText length];
}

Here's what is going on:

I check to see if the searchBar is blank, if it is, I load all of the objects into my results array and return.

Otherwise, I check to see if a character was deleted since last time the function was called, if so, I reload the results array with all of the objects.

I then run through the results list and make two comparisons (yes, I have to make two and it doesn't really speed things up if I only make one) and I keep an array of objects to remove that don't meet the criteria. Once it has completed checking all objects in the results list, it removes the objects that didn't match, calls a reloadData for the controller and stores the length of this search term.

By storing the length of the search term and removing objects from the result array, this speeds things up as the list gets narrowed down. Hoever, the first three keystrokes or so still have unacceptable lag.

Thanks in advance for any advice you may have,

Scott

PowerMac G5 Dual 1.8GHz

Posted on Apr 22, 2009 3:56 PM

Reply
5 replies

Apr 22, 2009 5:24 PM in response to K T

The list source is an NSMutableArray (all of the relevant lists are).

Also, digging a little deeper, this line is a big part of the problem:


NSRange range2 =[[NSString stringWithFormat:@"%d",gobo number]] rangeOfString:searchText options:NSCaseInsensitiveSearch;


Turns out that NSString stringWithFormat is really expensive used here. I replaced this with a comparison against a different NSString in my object and it sped things up by a factor of 3 (0.22 seconds now).

I'm still open to any other suggestion on how to make this better, though.

Thanks,

Scott

Apr 23, 2009 9:26 AM in response to sptrakesh

I haven't broken the list down into chunks due to the fact that I want the user to be able to get matches back that occur in the middle of the string. For example, if the user types in "Sun", I would like the result set to contain both "Sunday" and "African Sun". I don't think I can break up the riginal list and still get this functionality.

Search / filtering performance on iPhone

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.