NSTimer

http://www.lxweimin.com/p/9e7e8c806ea3


Note in particular that 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.

timer已經(jīng)被所加入的runloop強(qiáng)引用,你不需要再對(duì)它強(qiáng)引用,這樣用weak修飾,invalidate之后就會(huì)置為nil


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. Therefore, the actual time at which the timer fires potentially can be a significant period of time after the scheduled firing time. See also Timer Tolerance.
總結(jié):nstimer可能不會(huì)定時(shí)觸發(fā) 定時(shí)時(shí)間間隔為2秒,t1秒添加成功,那么會(huì)在t2、t4、t6、t8、t10秒注冊(cè)好事件,并在這些時(shí)間觸發(fā)。假設(shè)第3秒時(shí),執(zhí)行了一個(gè)超時(shí)操作耗費(fèi)了5.5秒,則觸發(fā)時(shí)間是:t2、t8.5、t10,第4和第6秒就被跳過(guò)去了,雖然在t8.5秒觸發(fā)了一次,但是下一次觸發(fā)時(shí)間是t10,而不是t10.5。


Repeating Versus Non-Repeating Timers
You specify whether a timer is repeating or non-repeating at creation time. A non-repeating timer fires once and then invalidates itself automatically, thereby preventing the timer from firing again. By contrast, a repeating timer fires and then reschedules itself on the same run loop.
A repeating timer always schedules itself based on the scheduled firing time, as opposed to the actual firing time. For example, if a timer is scheduled to fire at a particular time and every 5 seconds after that, the scheduled firing time will always fall on the original 5 second time intervals, even if the actual firing time gets delayed. If the firing time is delayed so far that it passes one or more of the scheduled firing times, the timer is fired only once for that time period; the timer is then rescheduled, after firing, for the next scheduled firing time in the future.

Repeating為No的timer fire一次即調(diào)用- invalidate方法,而yes的只要時(shí)機(jī)合適就會(huì)被觸發(fā),不管被延遲了多久


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.


@property NSTimeInterval tolerance;
默認(rèn)是0,the timer may fire at any time between its scheduled fire date and the scheduled fire date plus the tolerance. The timer will not fire before the scheduled fire date. For repeating timers, the next fire date is calculated from the original fire date regardless of tolerance applied at individual fire times, to avoid drift.

*scheduled time + tolerance > fire time > scheduled time *


-(void)invalidate;
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.
You must send this message from the thread on which the timer was installed. If you send this message from another thread, the input source associated with the timer may not be removed from its run loop, which could prevent the thread from exiting properly.

在哪個(gè)線程加入的runloop,在哪個(gè)線程將timer從runloop移除

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 一、什么是NSRunLoop NSRunLoop是消息機(jī)制的處理模式 NSRunLoop的作用在于有事情做的時(shí)候使...
    呦釋原點(diǎn)閱讀 679評(píng)論 0 2
  • 一、什么是NSRunLoop NSRunLoop是消息機(jī)制的處理模式 NSRunLoop的作用在于有事情做的時(shí)候使...
    KK_boy閱讀 523評(píng)論 0 0
  • 說(shuō)到NSTimer大家應(yīng)該都很熟悉,是的,我剛?cè)隝OS坑的第一個(gè)任務(wù)就是寫(xiě)一個(gè)找回密碼的界面和功能,點(diǎn)擊獲取驗(yàn)證碼...
    白熊閱讀 8,548評(píng)論 4 27
  • 這個(gè)55集的反腐電視劇,只剩下最后一集就算看完了,其實(shí)結(jié)局已經(jīng)不是那么重要了吧。這個(gè)反腐適合目前的大環(huán)境,所以很多...
    OCTA閱讀 265評(píng)論 0 0
  • wangshuang_ef30閱讀 139評(píng)論 0 0