objective-c - NSTimer 越来越落后
objective-c - NSTimer falling more and more behind
我有一个 NSTimer(运行 在主线程上),它应该每 0.02 秒关闭一次。但是,我注意到随着内存使用量开始上升(应用程序每个刻度捕获一帧并存储在数组中),后续刻度开始花费超过 0.02 秒。
我该如何解决这个问题?我开始认为 NSTimer 不适合这样的高频任务。
作为docs状态,
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.
由于 100 毫秒 = .1 秒,而您的计时器应该每 0.02 秒 运行,您的计时器计划远短于计时器的有效分辨率,因此您的计时器很容易不同步。
我有一个 NSTimer(运行 在主线程上),它应该每 0.02 秒关闭一次。但是,我注意到随着内存使用量开始上升(应用程序每个刻度捕获一帧并存储在数组中),后续刻度开始花费超过 0.02 秒。
我该如何解决这个问题?我开始认为 NSTimer 不适合这样的高频任务。
作为docs状态,
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.
由于 100 毫秒 = .1 秒,而您的计时器应该每 0.02 秒 运行,您的计时器计划远短于计时器的有效分辨率,因此您的计时器很容易不同步。