先吐槽一下这个标题,空格略蛋疼,不像中文,但是不写空格看上去则更诡异,求解决方案……

NSTimer会retain它的target,这样如果在控制器当中定义一个NSTimer,target指定为self,则会引起循环引用。

解决方案和防止block引用self一样,第一步需要把NSTimer的操作封装到一个block里,第二步则需要传递一个self的弱引用给block。

首先定义一个NSTimer的分类:

  1. #import <Foundation/Foundation.h>
  2.  
  3. @interface NSTimer (BlockSupport)
  4.  
  5. + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats;
  6.  
  7. @end
  8.  
  9. #import "NSTimer+BlockSupport.h"
  10.  
  11. @implementation NSTimer (BlockSupport)
  12.  
  13. + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats {
  14. return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(blockInvoke:) userInfo:[block copy] repeats:repeats];
  15. }
  16.  
  17. + (void)blockInvoke:(NSTimer *)timer {
  18. void (^block)() = timer.userInfo;
  19. if (block) {
  20. block();
  21. }
  22. }
  23.  
  24. @end

这个分类支持使用Block创建NSTimer,把操作传递到了万能对象userInfo里面,之后在控制器当中以这样的方式创建:

  1. __block typeof(self) weakSelf = self;
  2. _timer = [NSTimer scheduledTimerWithTimeInterval:0.5 block:^{
  3. [weakSelf doSth];
  4. } repeats:YES];

如此一来,NSTimer就不会令控制器的引用计数+1了。

防止 NSTimer retain 作为 target 的 self的更多相关文章

  1. ios - NSTimer中target的self是强引用问题

    当控制器ViewController跳转进入控制器OneViewController中的时候开启定时器,让定时器每隔一段时间打印一次,当OneViewController dismiss的时候,控制器 ...

  2. NSTimer的使用[zhuang]

    NSTimer 的头文件 /* NSTimer.h Copyright (c) 1994-2015, Apple Inc. All rights reserved. */ #import <Fo ...

  3. NSTimer 定时器总结

    一.初始化方法:有五种初始化方法,分别是 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation ...

  4. iOS 中的 NSTimer

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

  5. 【转】IOS NSTimer 定时器用法总结

    原文网址:http://my.oschina.net/u/2340880/blog/398598 NSTimer在IOS开发中会经常用到,尤其是小型游戏,然而对于初学者时常会注意不到其中的内存释放问题 ...

  6. 关于NSRunLoop和NSTimer的深入理解

    一.什么是NSRunLoop NSRunLoop是消息机制的处理模式 NSRunLoop的作用在于有事情做的时候使的当前NSRunLoop的线程工作,没有事情做让当前NSRunLoop的线程休眠 NS ...

  7. 小结OC中Retain cycle(循环引用)

    retain cycle 的产生 说到retain cycle,首先要提一下Objective-C的内存管理机制. 作为C语言的超集,Objective-C延续了C语言中手动管理内存的方式,但是区别于 ...

  8. NSTimer 详细设置

    NSTimer 详细设置1:http://blog.csdn.net/davidsph/article/details/7899483 NSTimer 详细设置2:http://blog.csdn.n ...

  9. iOS之 NSTimer(一)

    以前没怎么了解过这个NSTimer,其实还是有挺多坑的,今天来总结一下: 首先我们一起来看这个: 我在A  -> (push) -> B控制器,然后再B控制器中开启了一个NSTimer.然 ...

随机推荐

  1. ASP.NET MVC:多模板支持

    原文 http://www.cnblogs.com/happyframework/p/3224278.html 背景 准备写个博客练习一下WEB编程,有一个需求就是多模板支持,类似博客园的自定义模板一 ...

  2. 100个linux常用命令

    1,echo “aa” > test.txt 和 echo “bb” >> test.txt //>将原文件清空,并且内容写入到文件中,>>将内容放到文件的尾部 2 ...

  3. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

  4. MyEclipse中新建JSP(Advanced Template)文件时自动生成的

    <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="ca ...

  5. HDFS的运行原理(转载)

    原文地址:http://www.cnblogs.com/laov/p/3434917.html 简介 HDFS(Hadoop Distributed File System )Hadoop分布式文件系 ...

  6. 【Quick-COCOS2D-X 3.3 怎样绑定自己定义类至Lua之四】使用绑定C++至Lua的自己定义类

    续[Quick-COCOS2D-X 3.3 怎样绑定自己定义类至Lua之三]动手绑定自己定义类至Lua 之后.我们已经完毕了自己定义类至Lua的绑定.在接下来的环节,我们将使用它. 首先,我们须要确定 ...

  7. Codeforces 482B Interesting Array(线段树)

    题目链接:Codeforces 482B Interesting Array 题目大意:给定一个长度为N的数组,如今有M个限制,每一个限制有l,r,q,表示从a[l]~a[r]取且后的数一定为q,问是 ...

  8. 好用的侧边栏菜单/面板jQuery插件

    我想大家都用过一些APP应用,它们的菜单展示是以侧边栏滑动方式展现,感觉很新鲜,而现在网页设计也是如此,不少网站也效仿这样的方式来设计.使用侧边栏的好处就是可以节约空间,对于一些内容多或者喜欢简约的网 ...

  9. Oracle11g重建EM 报ORA-20001: SYSMAN already exists

    日志: Apr , :: PM oracle.sysman.emcp.EMReposConfig createRepository : SYSMAN already exists.. ORA-0651 ...

  10. MySQL半同步Semi-sync原理介绍【图说】

    上图先.