0

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?

Wingo
  • 5
  • 3
  • https://stackoverflow.com/a/43746977/6630644 – SPatel Mar 16 '23 at 10:08
  • Perhaps the calculations are too time consuming (that it can't be done in 1ms)? Doing `.dropFirst(1000 * 60).first()` only gave me about 0.16 seconds of error. – Sweeper Mar 16 '23 at 10:15
  • @SPatel using the linked solution returns error: 'any DispatchSourceTimer' cannot be used as a type conforming to protocol 'Publisher' because 'Publisher' has static requirements – Wingo Mar 16 '23 at 10:31

0 Answers0