DispatchSourceTimer и Swift 3.0

Я не могу понять, как заставить таймер отправки работать повторно в Swift 3.0. Мой код:

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

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

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

timer.resume()

таймер срабатывает только один раз и не повторяется, как и должно быть. Как я могу это исправить?

1 ответов


убедитесь, что таймер не выпадает из области видимости. В отличие от NSTimer, вам нужно поддерживать сильную ссылку на ваши таймеры GCD, например:

var timer: DispatchSourceTimer?

private func startTimer() {
    let queue = DispatchQueue(label: "com.firm.app.timer", attributes: .concurrent)

    timer?.cancel()        // cancel previous timer if any

    timer = DispatchSource.makeTimerSource(queue: queue)

    timer?.schedule(deadline: .now(), repeating: .seconds(5), leeway: .milliseconds(100))

    // or, in Swift 3:
    //
    // timer?.scheduleRepeating(deadline: .now(), interval: .seconds(5), leeway: .seconds(1))

    timer?.setEventHandler { [weak self] in // `[weak self]` only needed if you reference `self` in this closure and you want to prevent strong reference cycle
        print(Date())
    }

    timer?.resume()
}

private func stopTimer() {
    timer?.cancel()
    timer = nil
}