I try to implement Search as you type written in Swift. It works already but I need some tuning. I send with each letter typed in
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
let debouncedFindUser = debounce(
Static.searchDebounceInterval,
Static.q,
findUser)
debouncedFindUser()
}
a request to the backend.
func findUser() -> () {
SearchService.sharedInstance.getUser(0, numberOfItems: 100,
searchString: searchUserBar.text.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
,
success: {SearchUserDto -> Void in
if self.searchUserBar.text != nil {
self.updateSearch()
}
},
failure: {NSError -> Void in
})
}
I tried an implementation of Debounce found here How can I debounce a method call?
My Problem:
I want to "restart" a method call if it was triggered within a certain time. So let's exaggerate a little bit.
The request shall only start after search text wasn't typed for 2 seconds. So if I'm a quick typer and type each 0.5 seconds a letter the request should never be triggered except I pause for at least 2 seconds.