iOS 中的 NSTimer

NSTimer

fire

我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire 。比较想当然的做法是这样的:

  1. @interface DetailViewController ()
  2. @property (nonatomic, weak) NSTimer *timer;
  3. @end
  4. @implementation DetailViewController
  5. - (IBAction)fireButtonPressed:(id)sender {
  6. _timer = [NSTimer scheduledTimerWithTimeInterval:3.0f
  7. target:self
  8. selector:@selector(timerFire:)
  9. userInfo:nil
  10. repeats:YES];
  11. [_timer fire];
  12. }
  13. -(void)timerFire:(id)userinfo {
  14. NSLog(@"Fire");
  15. }
  16. @end

运行之后确实在控制台每隔3秒钟输出一次 Fire ,然而当我们从这个界面跳转到其他界面的时候却发现:控制台还在源源不断的输出着 Fire 。看来 Timer 并没有停止。

invalidate

既然没有停止,那我们在 DemoViewController 的 dealloc 里加上 invalidate 的方法:

  1. -(void)dealloc {
  2. [_timer invalidate];
  3. NSLog(@"%@ dealloc", NSStringFromClass([self class]));
  4. }

再次运行,还是没有停止。原因是 Timer 添加到 Runloop 的时候,会被 Runloop 强引用:

  1. Note in particular that run loops maintain strong references to their timers, so you dont have to maintain your own strong reference to a timer after you have added it to a run loop.

然后 Timer 又会有一个对 Target 的强引用(也就是 self ):

  1. Target is the object to which to send the message specified by aSelector when the timer fires. The timer maintains a strong reference to target until it (the timer) is invalidated.

也就是说 NSTimer 强引用了 self ,导致 self 一直不能被释放掉,所以也就走不到 self 的 dealloc 里。

既然如此,那我们可以再加个 invalidate 按钮:

  1. - (IBAction)invalidateButtonPressed:(id)sender {
  2. [_timer invalidate];
  3. }

嗯这样就可以了。(在 SOF 上有人说该在 invalidate 之后执行 _timer = nil ,未能理解为什么,如果你知道原因可以告诉我:)

在 invalidate 方法的文档里还有这这样一段话:

  1. 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.

NSTimer 在哪个线程创建就要在哪个线程停止,否则会导致资源不能被正确的释放。看起来各种坑还不少。

dealloc

那么问题来了:如果我就是想让这个 NSTimer 一直输出,直到 DemoViewController 销毁了才停止,我该如何让它停止呢?

  1. NSTimer 被 Runloop 强引用了,如果要释放就要调用 invalidate 方法。
  2. 但是我想在 DemoViewController 的 dealloc 里调用 invalidate 方法,但是 self 被 NSTimer 强引用了。
  3. 所以我还是要释放 NSTimer 先,然而不调用 invalidate 方法就不能释放它。
  4. 然而你不进入到 dealloc 方法里我又不能调用 invalidate 方法。
  5. 嗯…

HWWeakTimer

weakSelf

问题的关键就在于 self 被 NSTimer 强引用了,如果我们能打破这个强引用问题自然而然就解决了。所以一个很简单的想法就是:weakSelf:

  1. __weak typeof(self) weakSelf = self;
  2. _timer = [NSTimer scheduledTimerWithTimeInterval:3.0f
  3. target:weakSelf
  4. selector:@selector(timerFire:)
  5. userInfo:nil
  6. repeats:YES];

然而这并没有什么卵用,这里的 weak 和 strong 唯一的区别就是:如果在这两行代码执行的期间 self 被释放了, NSTimer 的 target 会变成 nil 。

target

既然没办法通过 __weak 把 self 抽离出来,我们可以造个假的 target 给 NSTimer 。这个假的 target 类似于一个中间的代理人,它做的唯一的工作就是挺身而出接下了 NSTimer 的强引用。类声明如下:

  1. @interface HWWeakTimerTarget : NSObject
  2. @property (nonatomic, weak) id target;
  3. @property (nonatomic, assign) SEL selector;
  4. @property (nonatomic, weak) NSTimer* timer;
  5. @end
  6. @implementation HWWeakTimerTarget
  7. - (void) fire:(NSTimer *)timer {
  8. if(self.target) {
  9. [self.target performSelector:self.selector withObject:timer.userInfo];
  10. } else {
  11. [self.timer invalidate];
  12. }
  13. }
  14. @end

然后我们再封装个假的 scheduledTimerWithTimeInterval 方法,但是在调用的时候已经偷梁换柱了:

  1. + (NSTimer *) scheduledTimerWithTimeInterval:(NSTimeInterval)interval
  2. target:(id)aTarget
  3. selector:(SEL)aSelector
  4. userInfo:(id)userInfo
  5. repeats:(BOOL)repeats {
  6. HWWeakTimerTarget* timerTarget = [[HWWeakTimerTarget alloc] init];
  7. timerTarget.target = aTarget;
  8. timerTarget.selector = aSelector;
  9. timerTarget.timer = [NSTimer scheduledTimerWithTimeInterval:interval
  10. target:timerTarget
  11. selector:@selector(fire:)
  12. userInfo:userInfo
  13. repeats:repeats];
  14. return timerTarget.timer;
  15. }

再次运行,问题解决。

block

如果能用 block 来调用 NSTimer 那岂不是更好了。我们可以这样来实现:

  1. + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
  2. block:(HWTimerHandler)block
  3. userInfo:(id)userInfo
  4. repeats:(BOOL)repeats {
  5. return [self scheduledTimerWithTimeInterval:interval
  6. target:self
  7. selector:@selector(_timerBlockInvoke:)
  8. userInfo:@[[block copy], userInfo]
  9. repeats:repeats];
  10. }
  11. + (void)_timerBlockInvoke:(NSArray*)userInfo {
  12. HWTimerHandler block = userInfo[0];
  13. id info = userInfo[1];
  14. // or `!block ?: block();` @sunnyxx
  15. if (block) {
  16. block(info);
  17. }
  18. }

这样我们就可以直接在 block 里写相关逻辑了:

  1. - (IBAction)fireButtonPressed:(id)sender {
  2. _timer = [HWWeakTimer scheduledTimerWithTimeInterval:3.0f block:^(id userInfo) {
  3. NSLog(@"%@", userInfo);
  4. } userInfo:@"Fire" repeats:YES];
  5. [_timer fire];
  6. }

嗯就是这样。

More

把上面的的代码简单的封装到了 HWWeakTimer 中,欢迎试用。

参考文献:

原文链接:http://blog.callmewhy.com/2015/07/06/weak-timer-in-ios/

本文出处刚刚在线:http://www.superqq.com/blog/2015/07/12/ios-zhong-de-nstimer/

iOS 中的 NSTimer的更多相关文章

  1. iOS中的NSTimer 和 Android 中的Timer

    首先看iOS的, Scheduling Timers in Run Loops A timer object can be registered in only one run loop at a t ...

  2. IOS中定时器NSTimer的开启与关闭

    调用一次计时器方法: myTimer = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(scro ...

  3. IOS中的NSTimer定时器详解

    /* 在IOS中有多种定时器,这里我对NSTimer定时器做了一个简单的介绍.如果你是小白,你可能会从这篇文章中学习到一些知识,如果你是大牛,请别吝啬你的评论,指出我的不足,你的质疑是对我最大的帮助. ...

  4. 【转】iOS中定时器NSTimer的使用

    原文网址:http://www.cnblogs.com/zhulin/archive/2012/02/02/2335866.html 1.初始化 + (NSTimer *)timerWithTimeI ...

  5. iOS中定时器NSTimer的使用-备用

    1.初始化 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelect ...

  6. iOS开发——高级篇——iOS 中的 NSTimer

    以前的老代码在使用 NSTimer 时出现了内存泄露 NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: 1 2 3 ...

  7. 【转】IOS中定时器NSTimer的开启与关闭

    原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...

  8. 【转】 IOS中定时器NSTimer的开启与关闭

    原文网址:http://blog.csdn.net/enuola/article/details/8099461 调用一次计时器方法: myTimer = [NSTimer scheduledTime ...

  9. ios 中定时器:NSTimer, CADisplayLink, GCD

    #import "ViewController.h" #import "RunloopViewController.h" @interface ViewCont ...

随机推荐

  1. 【转】github上值得关注的前端项目

    综合/资源 frontend-dev-bookmarks 一个巨大的前端开发资源清单.star:15000 front-end-collect 分享自己长期关注的前端开发相关的优秀网站.博客.以及活跃 ...

  2. Git 分支合并

    理解核心 Git最初只有一个分支,所有后续分支都是直接或间接的从这个分支切出来的. 在任意两个分支上,向前追溯提交记录,都能找到一个最近的提交同时属于这两个分支,这个提交就是两个分支的分叉节点 分支合 ...

  3. 二叉堆(二)之 C++的实现

    概要 上一章介绍了堆和二叉堆的基本概念,并通过C语言实现了二叉堆.本章是二叉堆的C++实现. 目录1. 二叉堆的介绍2. 二叉堆的图文解析3. 二叉堆的C++实现(完整源码)4. 二叉堆的C++测试程 ...

  4. How can I exclude directories from grep -R?

    ‘--exclude-dir=dir’ Exclude directories matching the pattern dir from recursive directory searches. ...

  5. [python]初探socket

    1.什么是socket? Socket中文译作:套接字,但是大家一般约定俗称的都用:socket.我想在解释socket是什么之前,先说它是用来干嘛的:socket是来建立'通信'的基础,建立连接,传 ...

  6. ActiveMQ学习(三)——MQ的通讯模式

    1) 点对点通讯:点对点方式是最为传统和常见的通讯方式,它支持一对一.一对多.多对多.多对一等多种配置方式,支持树状.网状等多种拓扑结构. 2) 多点广播:MQ适用于不同类型的应用.其中重要的,也是正 ...

  7. android 获取当前位置

    1. Android开发位置感知应用程序方式:1. GPS 定位     精确度高,仅适用于户外,严重消耗电量.如果手机内置GPS接受模块,即使手机处于信号盲区,依然可以获取位置信息. 2. NETW ...

  8. JavaScript--DOM修改元素的属性

    一旦你获得了要修改的元素,可以有2种方式,来读取和修改它的属性:一种老的方式(它被更多的用户代理所支持)和一种新的DOM方法的方式.老的和新的用户代理都允许你以对象属性的方式获取和设置元素的属性. 先 ...

  9. 《javascript高级程序设计》读书笔记1

    第二章 在HTML中引用javascript 1.<script>标签的位置:为了避免加载过多的JavaScript的脚本导致浏览器窗口一片空白.现代的web程序一般都把全部的 JavaS ...

  10. 关于c#的一些笔记

     序: 在vs中,可以生成三种项目: 第一种:控制台项目:用于练习C#语法 第二种:桌面程序项目:比如我们经常看到的桌面程序(CS). 第三种:web项目:用于开发网站 1.我们先来说一下.net和C ...