Skip to content

Instantly share code, notes, and snippets.

@rizumita
Created November 21, 2018 00:09
Show Gist options
  • Save rizumita/36d0090956518e06e19e6f48fa44446a to your computer and use it in GitHub Desktop.
Save rizumita/36d0090956518e06e19e6f48fa44446a to your computer and use it in GitHub Desktop.
SwiftでDispatchQueueでdebounce
extension DispatchQueue {
func debounce(interval: DispatchTimeInterval) -> (_ exec: @escaping () -> ()) -> DispatchSourceTimer? {
var timer: DispatchSourceTimer?
return { [weak self] exec in
timer?.cancel()
timer = DispatchSource.makeTimerSource(queue: self)
timer?.setEventHandler(handler: exec)
timer?.schedule(deadline: .now() + interval, repeating: .infinity)
timer?.resume()
return timer
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment