How can I debounce a method call?

前端 未结 13 1603
醉梦人生
醉梦人生 2020-11-30 01:18

I\'m trying to use a UISearchView to query google places. In doing so, on text change calls for my UISearchBar, I\'m making a request to google pla

13条回答
  •  广开言路
    2020-11-30 01:39

    If you like to keep things clean, here's a GCD based solution that can do what you need using familiar GCD based syntax: https://gist.github.com/staminajim/b5e89c6611eef81910502db2a01f1a83

    DispatchQueue.main.asyncDeduped(target: self, after: 0.25) { [weak self] in
         self?.findPlaces()
    }
    

    findPlaces() will only get called one time, 0.25 seconds after the last call to asyncDuped.

提交回复
热议问题