您的位置:首页 > 其它

NSTimer scheduledTimerWithTimeInterval与timerWithTimeInterval、initWithFireDate的区别

2015-05-06 14:20 573 查看
A timer object can be registered in only one run loop at a time, although it can be added to multiple run loop modes within that run loop. There are three ways to create a timer:

Use the
scheduledTimerWithTimeInterval:invocation:repeats:
or
scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:
class
method to create the timer and schedule it on the current run loop in the default mode.

Use the
timerWithTimeInterval:invocation:repeats:
or
timerWithTimeInterval:target:selector:userInfo:repeats:
class
method to create the timer object without scheduling it on a run loop. (After creating it, you must add the timer to a run loop manually by calling the
addTimer:forMode:
method
of the corresponding
NSRunLoop
object.)

Allocate the timer and initialize it using the
initWithFireDate:interval:target:selector:userInfo:repeats:
method.
(After creating it, you must add the timer to a run loop manually by calling the
addTimer:forMode:
method
of the corresponding
NSRunLoop
object.)

Once scheduled on a run loop, the timer fires at the specified interval until it is invalidated. A non-repeating timer invalidates itself immediately after it fires. However, for a repeating timer, you must invalidate the timer object yourself by calling its
invalidate
method.
Calling this method requests the removal of the timer from the current run loop; as a result, you should always call the
invalidate
method
from the same thread on which the timer was installed. Invalidating the timer immediately disables it so that it no longer affects the run loop. The run loop then removes the timer (and the strong reference it had to the timer), either just before the
invalidate
method
returns or at some later point. Once invalidated, timer objects cannot be reused.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: