关于NSTimer


NSTimer使用方式

常用的创建方式有

  • 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在创建一个NSTimer的时候会自动添加到Runloop当中。但是需要注意的是,这个方法会将NSTimer对象加入到NSDefaultRunLoopMode这个模式下。如果定时器运行同时还有滚动视图,那么还是需要手动添加到NSRunLoopCommonModes模式下。
方法2只创建一个NSTimer,并不会加入到Runloop循环中, 需要手动添加到Runloop循环中

官方文档:
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.)
同时也强调了,如果repeatsYES,则不需要重复添加到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设置为1, 那么结果将是在NSTimer对象创建完成后的一秒开始第一次执行selector:(SEL)aSelector中的aSelector。而- (void)fire方法就是相当于移除开始需要等待的1秒,立刻开始执行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设置了target:(id)aTarget和(我认为应该是“或”)userInfo:(nullable id)userInfo,也会删除对target:(id)aTargetuserInfo:(nullable id)userInfo的强引用。
在调用该方法的时候,必须在创建NSTimer的线程中,否则可能会造成与定时器关联的输入源可能不会从其运行循环中删除,这可能会阻止线程正常退出。

相关面试题

  • 两种创建NSTimer的类方法有什么区别?

答案很明显了,是否会自动添加到Runloop的区别。

  • NSTimer在日常使用中有什么需要注意的?
  • 1、可能会造成循环引用。原因是因为NSTimer会强引用target:(id)aTargetuserInfo:(nullable id)userInfo
  • 2、注意线程间通信。因为在子线程中创建NSTimer,selector:(SEL)aSelector方法也在子线程中执行。
  • 3、- (void)invalidate要在创建该NSTimer的线程中调用。
  • NSTimer会不会造成循环引用?怎么解决?

不一定。可重复的会造成循环引用,不可重复的不会造成循环引用。

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

- (void)viewWillDisappear:(BOOL)animated- (void)viewDidDisappear:(BOOL)animated中调用- (void)invalidate。或者根据自己的业务需求确定调用- (void)invalidate

解决方法二:(残缺的方法)

还有一种方法就是,换用block的方法。block的循环引用怎么解决肯定都知道吧。同理。
当然,这样控制器是会可以正常销毁没问题,但是定时器却没有被从Runloop中移除掉。至于定时器没有从Runloop中移除会有什么不良影响,还求大神指点。。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 关于NSTimer 在工作中经常会做一些延时任务,或者周期性任务,有时候也需要对取消延时任务操作。 延时任务一般有...
    CharType阅读 1,778评论 0 0
  • 想了解NSTimer的坑我们应该先熟悉下NSTimer的原理也就是他是怎么实现定时器的 runloop处理三种事件...
    RFeng阅读 4,121评论 0 2
  • NSTimer 一个计时器,不算常用但也算基础。我想每个开发iOS都应该知道,所以我一般会把他当作一个面试题。可以...
    jing091111阅读 2,812评论 5 2
  • iOS中计时器常用的有两种方式 使用NSTimer类(Swift 中更名为 Timer) NSTimer 常用的初...
    superDg阅读 5,836评论 0 1
  • 再一次面试中被问到nstimer的争取使用方法,原理,我当时就说了[_timer invalidate],time...
    iOS开发小平哥阅读 9,538评论 1 13

友情链接更多精彩内容