Search as you type Swift

后端 未结 3 708
眼角桃花
眼角桃花 2021-02-09 17:18

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: UISe         


        
相关标签:
3条回答
  • 2021-02-09 18:15

    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")
    }
    
    0 讨论(0)
  • 2021-02-09 18:16

    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() {
            ...
        }
    }
    
    0 讨论(0)
  • 2021-02-09 18:18

    The only thing u need is to create a Timer

      func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        let text = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
    
    // Create a Timer in this or parent class.
        debounceTimer.invalidate() // just in case this button is tapped multiple times
    
    
        // start the timer
        debounceTimer = Timer.scheduledTimer(timeInterval: 0.8, target: self, selector: #selector(self.startSearch(_:)), userInfo: text, repeats: false)
    }
    
    func startSearch(_ timer: Timer) {
        if let searchText = timer.userInfo as? String {
            print("Searching: \(searchText)")
    
            // make your service call here ...
        }
    }
    
    0 讨论(0)
提交回复
热议问题