Workout App: Foundation Timer vs Timer Publisher

Hi!
I was trying to develop a workout app on the Apple Watch by following the sample code Apple provides here.

I originally planned to use a standard Foundation Timer object to keep track of time:
Code Block
timer = Timer.scheduledTimer(
timeInterval: 1,
target: self,
selector: #selector(timerAction),
userInfo: nil,
repeats: true
)

but in the sample code they make use of Combine and of a Timer Publisher:
Code Block
start = Date()
cancellable = Timer.publish(every: 1, on: .main, in: .default)
.autoconnect()
.sink { [weak self] _ in
guard let self = self else { return }
}

It seems to be working both way but there's a problem on pausing the workout.
If I use a Foundation Timer and I pause the session it crashes on session.pause with multiple errors of this type:

Unable to transition to the desired state from the Paused(4) state (event 3). Allowed transitions from the current state are: {
  7 = "<error(7): Paused(4) -> Ended(3)>";
  6 = "<end(6): Paused(4) -> Ended(3)>";
  5 = "<stop(5): Paused(4) -> Stopped(6)>";
  4 = "<resume(4): Paused(4) -> Running(2)>";
}


On the other hand, if I use a Timer Publisher like in the sample code given above, everything works just fine.

I'm not familiar with Combine so I'm wondering why it's behaving like that. There's no apparent link between the timer object and the session object.
Thank you.

Replies

If you want an answer, you need to show the code that pauses the workout and the timer when using the Foundation timer.