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.
You could use an NSTimer. Start or invalidate and restart the timer for 2 seconds in your textDidChange. Have the timer fire the findUser method when it actually pops.
var debounceTimer: NSTimer?
@IBAction func test() {
if let timer = debounceTimer {
timer.invalidate()
}
debounceTimer = NSTimer(timeInterval: 2.0, target: self, selector: Selector("getUser"), userInfo: nil, repeats: false)
NSRunLoop.currentRunLoop().addTimer(debounceTimer!, forMode: "NSDefaultRunLoopMode")
}
func getUser() {
println("yeah")
}
The problem you're having is that you're debouncing the findUser
function before calling it every time. Your debouncing function sets up the initial timer and delay, but since you're calling it each time, the initial timer is always "now". You should debounce only once so the closure can maintain its captured last-executed time.
You want to only call debounce
one time and store it as a property, like so:
class MySearchController: ... {
lazy var debouncedFindUser = debounce(
Static.searchDebounceInterval,
Static.q,
self.findUser)
...
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
debouncedFindUser()
}
func findUser() {
...
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With