I have a UISearchBar in my app. As the user types any term, a dynamic search against an external API is performed..I send the entire search term as parameter to the API.
My question is, a user can type very quickly..So is there any way that instead of making a request to the API for every letter the user types, can we create a delay i.e. if the difference between typing 2 successive letters is extremely small, combine that into a single requests (rather than make 2 separate requests)
I currently use
[NSObject cancelPreviousPerformRequestsWithTarget:self];
[xmlParser performSelector:@selector(parseXMLFile:) withObject:url afterDelay:0.3];
But this is just creating a delay…I want the parseXMLFile to not be executed multiple times if the diff is very small (i.e. user types very quickly)
Use an NSTimer. For example, schedule a timer (with callback for search) to fire after 0.5 second, then if a new search term comes up before timer fires, invalidate it and reschedule.
If no new search within 0.5, then timer will fire and search will happen.