Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective-c - NSTimer falling more and more behind

I have a NSTimer (running on main thread) that is supposed to go off every 0.02s. However, I notice that as memory usage start going up (the app captures a frame every tick and stores in an array) subsequent ticks begin to take more then 0.02s.

How can I solve this issue? I'm starting to think NSTimer is not suited for high-frequency tasks like this.

like image 757
Andy Hin Avatar asked Dec 04 '25 13:12

Andy Hin


1 Answers

As the docs state,

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.

Since 100 milliseconds = .1 seconds and your timer is supposed to run every 0.02 seconds, your timer schedule is far shorter than the timer's effective resolution and so you timer can easily get out of sync.

like image 185
Lyndsey Scott Avatar answered Dec 07 '25 15:12

Lyndsey Scott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!