你真的会用NSTimer吗?你真的了解NSTimer的运行机制和生命周期吗?我估计部分人都是一知半解,只是会用,至于引发的问题,都没去管。今天我来仔细分析一下怎么用NSTimer,以及它的原理。
定义:
A timer that fires after a certain time interval has elapsed, sending a specified message to a target object.
Timers work in conjunction with run loops. Run loops maintain strong references to their timers, so you don’t have to maintain your own strong reference to a timer after you have added it to a run loop.
To use a timer effectively, you should be aware of how run loops operate. See Threading Programming Guide for more information.
A timer is not a real-time mechanism. If a timer’s firing time occurs during a long run loop callout or while the run loop is in a mode that isn't monitoring the timer, the timer doesn't fire until the next time the run loop checks the timer. Therefore, the actual time at which a timer fires can be significantly later. See also Timer Tolerance.
NSTimer is toll-free bridged with its Core Foundation counterpart, CFRunLoopTimerRef. See Toll-Free Bridging for more information.
注意点1:
Timers work in conjunction with run loops. Run loops maintain strong references to their timers, so you don’t have to maintain your own strong reference to a timer after you have added it to a run loop.
上边告诉我们定时器所在的runloop会维持定时器的强引用,也就是会强引用定时器,我们没有必要去强引用这个定时器。
但是有时候,由于我们需要把定时器作为全局变量来使用,因为其他地方也会用到它。我们会采用两种方案,要么把它声明为全局变量,要么把它声明属性。这样做其实都代表当前类对定时器进行了强引用。但是这不需要我们当心,因为系统会在dealloc的时候,对定时器的引用计数减一。
需要当心的是,当前实例销毁之后,这个定时器并没有销毁,因为添加定时器的runloop依然对定时器进行着强引用,定时器依然会活的好好的。
那怎样解决呢?
我们需要在当前实例销毁的时候,执行以下代码:
- (void)dealloc{
[_timer invalidate];
}
这样做的好处是,在当前类的实例销毁之后,定时器也会被系统回收。
上述代码为什么可以销毁定时器呢?
- (void)invalidate;
Stops the timer from ever firing again and requests its removal from its run loop.
This method is the only way to remove a timer from an NSRunLoop object. The NSRunLoop object removes its strong reference to the timer, either just before the invalidate method returns or at some later point.
If it was configured with target and user info objects, the receiver removes its strong references to those objects as well.
apple文档告诉我们,执行这个方法之后,定时器所在的runloop会移除对定时器的强引用,因此定时器会销毁。
注意点2:
A timer is not a real-time mechanism. If a timer’s firing time occurs during a long run loop callout or while the run loop is in a mode that isn't monitoring the timer, the timer doesn't fire until the next time the run loop checks the timer. Therefore, the actual time at which a timer fires can be significantly later. See also Timer Tolerance.
定时器不准,定时器不是一种实时的机制。上边介绍了两种不准确的情况:1.定时器在很长的runloop之后才开启;2.定时器添加时是runloop在一种模式下,在runloop的其他模式下并不会监听定时器,例如:在default模式下添加,那么在tracking模式下并不会调用定时器。因此,定时器会出现延时。