防止 NSTimer retain 作为 target 的 self
先吐槽一下这个标题,空格略蛋疼,不像中文,但是不写空格看上去则更诡异,求解决方案……
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的更多相关文章
- ios - NSTimer中target的self是强引用问题
当控制器ViewController跳转进入控制器OneViewController中的时候开启定时器,让定时器每隔一段时间打印一次,当OneViewController dismiss的时候,控制器 ...
- NSTimer的使用[zhuang]
NSTimer 的头文件 /* NSTimer.h Copyright (c) 1994-2015, Apple Inc. All rights reserved. */ #import <Fo ...
- NSTimer 定时器总结
一.初始化方法:有五种初始化方法,分别是 + (NSTimer *)timerWithTimeInterval:(NSTimeInterval)ti invocation:(NSInvocation ...
- iOS 中的 NSTimer
iOS 中的 NSTimer NSTimer fire 我们先用 NSTimer 来做个简单的计时器,每隔5秒钟在控制台输出 Fire .比较想当然的做法是这样的: @interface Detail ...
- 【转】IOS NSTimer 定时器用法总结
原文网址:http://my.oschina.net/u/2340880/blog/398598 NSTimer在IOS开发中会经常用到,尤其是小型游戏,然而对于初学者时常会注意不到其中的内存释放问题 ...
- 关于NSRunLoop和NSTimer的深入理解
一.什么是NSRunLoop NSRunLoop是消息机制的处理模式 NSRunLoop的作用在于有事情做的时候使的当前NSRunLoop的线程工作,没有事情做让当前NSRunLoop的线程休眠 NS ...
- 小结OC中Retain cycle(循环引用)
retain cycle 的产生 说到retain cycle,首先要提一下Objective-C的内存管理机制. 作为C语言的超集,Objective-C延续了C语言中手动管理内存的方式,但是区别于 ...
- NSTimer 详细设置
NSTimer 详细设置1:http://blog.csdn.net/davidsph/article/details/7899483 NSTimer 详细设置2:http://blog.csdn.n ...
- iOS之 NSTimer(一)
以前没怎么了解过这个NSTimer,其实还是有挺多坑的,今天来总结一下: 首先我们一起来看这个: 我在A -> (push) -> B控制器,然后再B控制器中开启了一个NSTimer.然 ...
随机推荐
- ZigBee技术简介
Zigbee的由来 在蓝牙技术的使用过程中,人们发现蓝牙技术尽管有许多优点,但仍存在许多缺陷.对工业,家庭自动化控制和遥测遥控领域而言,蓝牙技术显得太复杂,功耗大,距离近,组网规模太小等,……而工业自 ...
- Linux进程间通信——使用匿名管道
在前面,介绍了一种进程间的通信方式:使用信号,我们创建通知事件,并通过它引起响应,但传递的信息只是一个信号值.这里将介绍另一种进程间通信的方式——匿名管道,通过它进程间可以交换更多有用的数据. 一 ...
- mfc窗口,父窗口parentwindow,所有者窗口ownerwindow 区别
一. parent:创建者,owner:所有者 小玉的父母生下小玉,养到8岁,卖给贾府当丫头小玉的父母是parent,贾府是owner 二. 1.Pop-up窗口: 一个弹出窗口是必须具有WS_POP ...
- (转)flash的Socket通讯沙箱和安全策略问题
一.沙箱和安全策略问题 1.此问题发生在连接时,准确地说是连接前,分别两种情况: 1.本地播放 本地播放时,默认情况下Flash Player将不允许swf访问任何网络. 访问http://www.m ...
- ios消息的交互方式
注意这些都是界面回传(即从第二个界面传到第一个界面,从第一个界面传到第二个界面的时候用第二个界面的属性即可) iOS消息的交互方式有4种,分别为:通知,代理,block,kvo 现在我们对这个4中 ...
- C# 循环的应用 手机号抽奖 选班长的练习题
//第一题 //20个手机号滚动显示随机抽出一个中奖号码来: static void bbb(string[] args) { string[] cellPhone = new string[] { ...
- Java与C++相异的地方
继承标识:Java使用extends/implement,C++使用: super:调用父类的某些东西 instanceof:RTTI机制(A is instanceif B) final:类似于C+ ...
- [Linked List]Linked List Cycle,Linked List Cycle II
一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...
- Subsets,Subsets II
一.Subsets Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a s ...
- Node.js包管理器:
Node.js包管理器: 当我们要把某个包作为工程运行的一部分时,通过本地模式获取,如果要在命令行下使用,则使用全局模式安装 使用全局模式安装的包并不能直接在JavaScript文件中用require ...