關(guān)于NSTimer


NSTimer使用方式

常用的創(chuàng)建方式有

  • 1
    + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
    + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block;

  • 2
    + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;
    + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval repeats:(BOOL)repeats block:(void (^)(NSTimer *timer))block;

方法1在創(chuàng)建一個NSTimer的時候會自動添加到Runloop當(dāng)中。但是需要注意的是,這個方法會將NSTimer對象加入到NSDefaultRunLoopMode這個模式下。如果定時器運行同時還有滾動視圖,那么還是需要手動添加到NSRunLoopCommonModes模式下。
方法2只創(chuàng)建一個NSTimer,并不會加入到Runloop循環(huán)中, 需要手動添加到Runloop循環(huán)中

官方文檔:
You must add the new timer to a run loop, using addTimer:forMode:. Then, after ti
seconds have elapsed, the timer fires, sending the message aSelector
to target
. (If the timer is configured to repeat, there is no need to subsequently re-add the timer to the run loop.)
同時也強(qiáng)調(diào)了,如果repeatsYES,則不需要重復(fù)添加到runloop里。

常用方法

  • - (void)fire;

官方文檔:
You can use this method to fire a repeating timer without interrupting its regular firing schedule. If the timer is non-repeating, it is automatically invalidated after firing, even if its scheduled fire date has not arrived.

比如我們把TimeInterval:(NSTimeInterval)ti中的ti設(shè)置為1, 那么結(jié)果將是在NSTimer對象創(chuàng)建完成后的一秒開始第一次執(zhí)行selector:(SEL)aSelector中的aSelector。而- (void)fire方法就是相當(dāng)于移除開始需要等待的1秒,立刻開始執(zhí)行selector:(SEL)aSelector中的aSelector。

  • - (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.
  • Special Considerations
    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.

官方文檔寫的很清楚:
此方法是停止定時器,并將它從Runloop中移除掉。
如果NSTimer設(shè)置了target:(id)aTarget和(我認(rèn)為應(yīng)該是“或”)userInfo:(nullable id)userInfo,也會刪除對target:(id)aTargetuserInfo:(nullable id)userInfo的強(qiáng)引用。
在調(diào)用該方法的時候,必須在創(chuàng)建NSTimer的線程中,否則可能會造成與定時器關(guān)聯(lián)的輸入源可能不會從其運行循環(huán)中刪除,這可能會阻止線程正常退出。

相關(guān)面試題

  • 兩種創(chuàng)建NSTimer的類方法有什么區(qū)別?

答案很明顯了,是否會自動添加到Runloop的區(qū)別。

  • NSTimer在日常使用中有什么需要注意的?
  • 1、可能會造成循環(huán)引用。原因是因為NSTimer會強(qiáng)引用target:(id)aTargetuserInfo:(nullable id)userInfo
  • 2、注意線程間通信。因為在子線程中創(chuàng)建NSTimer,selector:(SEL)aSelector方法也在子線程中執(zhí)行。
  • 3、- (void)invalidate要在創(chuàng)建該NSTimer的線程中調(diào)用。
  • NSTimer會不會造成循環(huán)引用?怎么解決?

不一定??芍貜?fù)的會造成循環(huán)引用,不可重復(fù)的不會造成循環(huán)引用。

解決方法一:(最完美的方法)

- (void)viewWillDisappear:(BOOL)animated- (void)viewDidDisappear:(BOOL)animated中調(diào)用- (void)invalidate?;蛘吒鶕?jù)自己的業(yè)務(wù)需求確定調(diào)用- (void)invalidate

解決方法二:(殘缺的方法)

還有一種方法就是,換用block的方法。block的循環(huán)引用怎么解決肯定都知道吧。同理。
當(dāng)然,這樣控制器是會可以正常銷毀沒問題,但是定時器卻沒有被從Runloop中移除掉。至于定時器沒有從Runloop中移除會有什么不良影響,還求大神指點。。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 關(guān)于NSTimer 在工作中經(jīng)常會做一些延時任務(wù),或者周期性任務(wù),有時候也需要對取消延時任務(wù)操作。 延時任務(wù)一般有...
    CharType閱讀 279評論 0 0
  • 想了解NSTimer的坑我們應(yīng)該先熟悉下NSTimer的原理也就是他是怎么實現(xiàn)定時器的 runloop處理三種事件...
    RFeng閱讀 834評論 0 2
  • NSTimer 一個計時器,不算常用但也算基礎(chǔ)。我想每個開發(fā)iOS都應(yīng)該知道,所以我一般會把他當(dāng)作一個面試題??梢?..
    jing091111閱讀 411評論 5 2
  • iOS中計時器常用的有兩種方式 使用NSTimer類(Swift 中更名為 Timer) NSTimer 常用的初...
    superDg閱讀 1,944評論 0 1
  • 再一次面試中被問到nstimer的爭取使用方法,原理,我當(dāng)時就說了[_timer invalidate],time...
    iOS開發(fā)小平哥閱讀 4,198評論 1 13

友情鏈接更多精彩內(nèi)容