I can't figure out how to make dispatch timer work repeatedly in Swift 3.0. My code:
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()
Timer just fires one time and doesn't repeat itself like it should be. How can I fix this?
Make sure the timer doesn't fall out of scope. Unlike Timer
(where the RunLoop
on which you schedule it keeps the strong reference until the Timer
is invalidated), you need to maintain your own strong reference to your GCD timers, e.g.:
private var timer: DispatchSourceTimer? private func startTimer() { let queue = DispatchQueue(label: "com.firm.app.timer", attributes: .concurrent) timer = DispatchSource.makeTimerSource(queue: queue) 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?.schedule(deadline: .now(), repeating: .seconds(5), leeway: .milliseconds(100)) timer?.resume() } private func stopTimer() { timer = nil }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With