Timers
Timer and DispatchSourceTimer are fully supported on Android. Timers scheduled on the main run loop fire on the Android UI thread.
Schedule a repeating timer on the main run loop:
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in print("tick")}Schedule a one-shot timer:
Timer.scheduledTimer(withTimeInterval: 5.0, repeats: false) { _ in performDelayedAction()}DispatchSourceTimer
Section titled “DispatchSourceTimer”Use DispatchSourceTimer for more control over scheduling:
let timer = DispatchSource.makeTimerSource(queue: .main)timer.schedule(deadline: .now() + 1.0, repeating: 2.0)timer.setEventHandler { print("timer fired")}timer.resume()Cancel the timer when done:
timer.cancel()