1

I have a timer scheduled as following:

private var timerCancellable: Cancellable?

timerCancellable = Timer
        .publish(every: 1.0, on: .current, in: .common)
        .autoconnect()
        .scan(-1) { counter, _ in counter + 1 }
        .sink { counter in
            print("\(counter)")
            
        }

This works fine (print out 0 1 2 3 ...) when I test it on any normal view controller. But in a specific view controller which I a live camera feed with Metal shaders (and several different DispatchQueue instances), it gives this output:

0 0 1 2 3 ...

0 is repeated twice. I tried .main for RunLoop but the result is the same. Can anyone help?

Asteroid
  • 1,049
  • 2
  • 8
  • 16
  • 1
    It seems unlikely that this would be debuggable without a [mre]. – jnpdx Dec 10 '21 at 18:52
  • I would guess it's initiated twice. However, if you are trying to calculate elapsed time like that this is extremely imprecise. – Sulthan Dec 10 '21 at 19:18
  • It's initiated only once @Sulthan – Asteroid Dec 10 '21 at 19:22
  • Why are you trying to do with the timer? As @Sulthan says, counters and `Timer` are not a good way of tracking elapsed or remaining time. They suffer from inaccuracy and jitter. You should use a faster timer (for smoothness) and a `Date` from which you can track elapsed or remaining time – Paulw11 Dec 10 '21 at 22:06
  • Forget about why I want this timer, it's not the question. – Asteroid Dec 10 '21 at 23:16

0 Answers0