Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delay UISearchbar parsing

I have a UISearchbar in my app. This is a dynamic search and as the user enters text, a remote database is searched via a remote API call (I think it is through REST).

The table view gets refreshed dynamically, as the user types. I am using NSXMLParser for parsing the XML results. (so 3 delegate methods; didStartElement, didEndElement)

In some cases, there are duplicate entries shown in the results e.g. If user has typed YAH, it shows YAHOO 3-4 times. I'm not sure why.

How can I reduce the number of times the parsing is done, or how to delay the parsing, so that it does not make a request for every character entered/deleted by the user.

This, I am assuming, might fix the problem.

like image 923
hmthur Avatar asked Dec 28 '22 02:12

hmthur


1 Answers

One thing you can do is introduce a delay before you send off the remote API call, instead of sending one query for every character.

// Whenever UISearchbar text changes, schedule a lookup
- (void)searchBar:(UISearchBar *)theSearchBar textDidChange:(NSString *)text {
        // cancel any scheduled lookup
        [NSObject cancelPreviousPerformRequestsWithTarget:self];
        // start a new one in 0.3 seconds
        [self performSelector:@selector(doRemoteQuery) withObject:nil afterDelay:0.3];
}
like image 178
David Gelhar Avatar answered Jan 13 '23 02:01

David Gelhar