Building a Precision Timer in SwiftUI Without Drift
The Problem With Naive Timers When I started building BoxTime, my boxing round timer app, I made the classic mistake: I used Timer.scheduledTimer with a 1-second interval and decremented a counter....

Source: DEV Community
The Problem With Naive Timers When I started building BoxTime, my boxing round timer app, I made the classic mistake: I used Timer.scheduledTimer with a 1-second interval and decremented a counter. It looked right in the simulator. It looked right for the first 30 seconds on a real device. Then the drift crept in. After a 3-minute round, my timer was off by 1-2 seconds. Over a full 12-round session, that adds up. In boxing, timing matters. You can't have a round timer that lies to you. Why Timers Drift The fundamental issue is that Timer in iOS is not a precision instrument. It fires on the run loop, and the run loop has other things to do. Each tick might be 1.002 seconds instead of 1.000. Those fractions accumulate. // The naive approach - DO NOT use this for precision Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { _ in self.remainingSeconds -= 1 } The run loop might delay a fire by milliseconds. When the UI is busy, it gets worse. Scrolling, animations, haptics -- all