I'm doing a training application that connects to a sensor to track data while the user hits a punching bag. In the view that manages the actual training session, I created a view that would manage the updating of the view based on a Timer. It's 600 lines of code so im not gonna be able to just copy paste the entire thing, basically its structured like this:
I have a timer
let timer = Timer
.publish(every: 0.001, on: .main, in: .common)
.autoconnect()
And I have a VStack like this
VStack{
// Bunch of graphical elements that update based on calculation made in the onReceive()
}.onReceive(timer) { time in
// all sorts of calculations
}
This worked great, until I noticed that the timer doesn't stay accurate, which is pretty needed otherwise a 1 minute session takes 1 minute and 10 seconds or more, defeating the whole purpouse.
So, my question is: How can I make the timer precise without changing the whole structure of my code? I read online that you should take note of the starting date and calculate time elapsed from that date, so I tried to implement a class and ObservableObject that would work like this, problem is that the ObservableObject makes use of a Timer himself to work and I can't run both timers at once, when one starts the other stops.
I also tried implementing a TimelineView without success.
Is there any way to just keep it on onReceive and just make it not delay?