1

This may be a bit of a silly question, but I'm looking for a reliable way to create a repeating timer in Swift that's as completely accurate as possible. I've researched this quite a bit and have found that the mach_absolute_time api seems to be what I'm looking for.

However, while I think I understand mach_absolute_time, there's not much out there on mach_wait_until (if I understand correctly that's the mach api version of using Timer.scheduledTimer() to call a selector).

So my goal is to turn something like Martin R's answer to this post: Measuring Time Accurately in Swift for Comparison Across Devices into a timer that can measure out an exact amount of time, call a selector, and repeat with little to no loss of time between ticks.

Thanks!

Update

The other thing I ran into is that the timer can't lock up the app while the it's running either – I know since mach is pretty low level, calling mach_wait_until will stop everything else and wait for the time to elapse (I think).

sreveq
  • 45
  • 7
  • Did you read [TN2169 High Precision Timers in iOS / OS X](https://developer.apple.com/library/content/technotes/tn2169/_index.html)? It contains (among other advice) sample code using mach_wait_until. – Martin R Jul 03 '17 at 11:49
  • I did - see update to question :) – sreveq Jul 03 '17 at 12:07

0 Answers0