iPhone SDK - Search Bar Thread Synchronization Question
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, 5开发者_高级运维00ms after a keypress (unless there has been another keypress in the meantime, then reset). Is that possible?
Thanks for any help.
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.
精彩评论