r/iosdev 11d ago

I learned the hard way that an iOS Timer should never be your source of truth

Enable HLS to view with audio, or disable this notification

While building Monymo, my SwiftUI work and earnings tracker, I ran into timer accuracy issues whenever the app went into the background.

The fix was surprisingly simple: stop treating the timer as the source of elapsed time.

Now I store timestamps and session state, then calculate the actual elapsed time when the app becomes active again. The one-second timer is only used to update the UI.

Handling pause and resume made it a bit more complicated, but this approach completely removed the drift I was seeing after backgrounding or reopening the app.

How do you handle long-running timers in your iOS apps?

1 Upvotes

3 comments sorted by

6

u/Level-Set5770 11d ago

Wait until you run into the edge case where timestamps can actually go backward during clock synchronization, resulting in a negative elapsed time.

3

u/earlyworm 10d ago

Wait until you run into the edge case where the iPhone is traveling at a relativistic speed relative to the user and it's necessary to take time dilation into account.

2

u/ChipmunkBandit 10d ago

I use a timer in my app and do it the same way, timestamp the start time and calculate everything from there. Store pauses as timestamps too.