Question

I am trying to determine how to get UISearchBar to delay calling textDidChange till a few millisecs have passed. My goal is to aggregate multiple keyboard entries into one remote web service call.. As I have it now I call the server every keystroke and this is way too much.

If there is not some way within one of the UISearchBar Delegate methods... is there some other way to accomplish the same goal... namely just do not call the server until the user has paused entering keys?

I have been using this delegate method:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 
Was it helpful?

Solution

Yes, you can use an NSTimer.

Do this:

  1. Create an NSTimer that calls your remote web service call and run it when the user starts typing.
  2. Each time the user enters a key have the timer reset and start again.
  3. If the user stopped typing the NSTimer will fire your web service call.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top