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

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

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

首先定义一个NSTimer的分类:

 #import <Foundation/Foundation.h>

 @interface NSTimer (BlockSupport)

 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats;

 @end

 #import "NSTimer+BlockSupport.h"

 @implementation NSTimer (BlockSupport)

 + (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void(^)())block repeats:(BOOL)repeats {
return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(blockInvoke:) userInfo:[block copy] repeats:repeats];
} + (void)blockInvoke:(NSTimer *)timer {
void (^block)() = timer.userInfo;
if (block) {
block();
}
} @end

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

     __block typeof(self) weakSelf = self;
_timer = [NSTimer scheduledTimerWithTimeInterval:0.5 block:^{
[weakSelf doSth];
} 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. C# Chart 折线图 多条数据展示

    private void btn_Click(object sender, EventArgs e) { DBHelper db = new DBHelper(); DataSet ds = db.G ...

  2. 关于C#的一点小知识 以后自己用

    项目过程中遇到C#代码的编写 上网查之后的结果 @html.ActionLink的几种参数格式 一 Html.ActionLink("linkText","actionN ...

  3. [置顶] mkdir函数-linux

    tyle="margin:20px 0px 0px; font-size:14px; line-height:26px; font-family:Arial">

  4. PDM使用问题总结

    1.连接postgres生成pdm 直接连postgres数据库生成的可以生成表名,但表结构为空,不知为何,后来直接用生成数据库sql脚本后转成pdm成功.但是列注释没有了. 2.sql语句生成的pd ...

  5. 栈ADT的数组实现

    /* 栈的数组实现声明 */ struct StackRecord; typedef struct StackRecord *Stack; #define MinSstackSize 5 #defin ...

  6. 关键路径(CriticalPath)算法

    #include <stdio.h> #include <stdlib.h> #include <malloc.h> #define MAXVEX 30 //最大顶 ...

  7. C语言当中的作用域

    在C语言当中,变量的作用域分为两种:全局变量和局部变量. 在所有函数之外声明的变量是全局变量,这些变量可以在整个程序当中被访问: 局部变量是在某一对大括号({})之间生命的变量,这些变量在这对大括号之 ...

  8. C#中Abstract和Virtual 【转】

    http://www.cnblogs.com/blsong/archive/2010/08/12/1798064.html 在C#的学习中,容易混淆virtual方法和abstract方法的使用,现在 ...

  9. JAVA程序猿面试题汇总

    (此话题将不断更新,请留意) 第一,谈谈final, finally, finalize的差别. final 用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承.finally是异常 ...

  10. windows+Ubuntu双系统 windows引导修复

    我的博客:http://blog.csdn.net/muyang_ren 装完windows+Ubuntu麒麟双系统后,发现引导是Ubuntu的. Ubuntu的引导是GRUP windows的引导是 ...