LoginSignup
18
21

More than 5 years have passed since last update.

[iOS]正確的なタイマーは?

Posted at

今日NSTimerのReferenceをよく読んでみたら、一つ今まで知らなかったことを見つかりました。

A timer is not a real-time mechanism; it fires only when one of the run loop modes to which the timer has been added is running and able to check if the timer’s firing time has passed. Because of the various input sources a typical run loop manages, the effective resolution of the time interval for a timer is limited to on the order of 50-100 milliseconds. If a timer’s firing time occurs during a long callout or while the run loop is in a mode that is not monitoring the timer, the timer does not fire until the next time the run loop checks the timer.

この内容を簡単に翻訳すると、NSTimerはリアタイム的なものではありません。NSTimerが実行されているRunLoopに依存しています。タイマーの精度は50-100ミリ秒。長い呼び出しの中やRunLoopがNSTimerをモニュターリングしていない場合、RunLoopが次回NSTimerをチェックするまでNSTimerがfireしません。

ということでNSTimerは実行するRunLoopによって時間的な精度が大幅に揺れていることが分かりました。

精密なタイマーを実装したい場合はどうしますか?
こちら→how to get a accurate timer in ios 結構議論していたみたいです。

勧めたのはdispatch_afterかCADisplayLinkでした。
どちらがもっといいかはわかりません、今度試してみます。

18
21
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
21