Kevin Kevin - 1 year ago 84
Swift Question

Why would a `scheduledTimer` fire properly when setup outside a block, but not within a block?

The following code snippet works perfectly when called outside a completion block, but the timer is never fired when I set it up inside the block. I don't understand why there is a difference:

self.timer = Timer.scheduledTimer(timeInterval: 1,
target: self,
selector: #selector(,
userInfo: nil,
repeats: true)

I was not using the self references when calling it initially outside the block, but then once inside, it was required. However I tested the exact same code outside the block again and it does still work.

The block is a completion hander that is called after asking permission for
related information.

Rob Rob
Answer Source

The issue is that the completion block in question was probably not running on the main thread and therefore didn't have a run loop. But timers need to be scheduled on a run loop, and while the main thread has one, most background threads do not (unless you add one, yourself).

To fix this, in that completion handler, dispatch the creation of the timer back to the main thread and it should work fine:

DispatchQueue.main.async {
    self.timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(ViewController.handleTimer(_:)), userInfo: nil, repeats: true)

Or use a dispatch source timer (a timer that can be scheduled for a background queue, and doesn't require a run loop).

var timer: DispatchSourceTimer!

func startTimer() {
    let queue = DispatchQueue(label: "")
    timer = DispatchSource.timer(queue: queue)
    timer.setEventHandler {
        // do something
    timer.scheduleRepeating(deadline: .now(), interval: 1.0)