Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Search as you type Swift

Tags:

search

ios

swift

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.

like image 710
S. Birklin Avatar asked Feb 09 '15 14:02

S. Birklin


2 Answers

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")
}
like image 81
Jeremy Pope Avatar answered Oct 09 '22 21:10

Jeremy Pope


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() {
        ...
    }
}
like image 37
Nate Cook Avatar answered Oct 09 '22 20:10

Nate Cook