Derreck Derreck - 10 months ago 526
Swift Question

DispatchSourceTimer and Swift 3.0

I can't figure out how to make dispatch timer work repeatedly in Swift 3.0. My code:

let queue = DispatchQueue(label: "",
attributes: DispatchQueue.Attributes.concurrent)
let timer = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(rawValue: UInt(0)),
queue: queue)

interval: .seconds(5),
leeway: .seconds(1)

timer.setEventHandler(handler: {
//a bunch of code here


Timer just fires one time and doesn't repeat itself like it should be. How can I fix this?

Rob Rob
Answer Source

Make sure the timer doesn't fall out of scope. Unlike NSTimer, you need to maintain a strong reference to your GCD timers, e.g.:

var timer: DispatchSourceTimer?

private func startTimer() {
    let queue = DispatchQueue(label: "", attributes: .concurrent)
    timer = DispatchSource.makeTimerSource(flags: [], queue: queue)

    timer?.scheduleRepeating(deadline: .now(), interval: .seconds(5), leeway: .seconds(1))

    timer?.setEventHandler {


private func stopTimer() {
    timer = nil