views:

257

answers:

1

This is more of a high level "how do you solve this type of problem" type of question, as opposed to a "why doesn't my code compile" question.

I've got a search bar within an iPhone application that searches my data just fine. However, i have the search triggered (handleSearchForTerm is called) after each keypress. This creates some lag.

I like the idea of having search results appear automatically, but I don't like that for larger datasets, the act of searching appears to interrupt user input.

Is this something I can't fix aside from making my search routine faster? Or, is there a better way to handle this?

One thought is that I'd like to be able to trigger a search only, say, 500ms after a keypress (unless there has been another keypress in the meantime, then reset). Is that possible?

Thanks for any help.

+1  A: 

You can use the performSelector:withObject:afterDelay to call the search routine after a delay of 500ms

EDIT (after your comment):

You can use cancelPreviousPerformRequestsWithTarget: selector: object: to cancel subsequent calls to the search routine

Ofcourse, you will need some kind of mechanism to identify if a search routine is currently in process. Use semaphores for that.

Mihir Mathuria
If multiple performSelectors are called to do the same task (with different objects) does only the last fire? Or do I need to implement some kind of mechanism to get only the last request to be performed?
Matt
Please check modified answer
Mihir Mathuria