NSTimer作为一个经常使用的类,却有一个最大的弊病,就是会强引用target。造成调用timer很麻烦。稍有不慎就造成内存泄漏。

下面就是为解决问题做的封装。

直接上代码:

#import <Foundation/Foundation.h>

@interface LZLTimer :
NSObject

-(void)startTimerInterval:(NSTimeInterval)ti target:aTarget selector:(SEL)selector
userInfo:(id)userInfo repeats:(BOOL)yesOrNo;

@end

#import "LZLTimer.h"

@interface LZLWeakTimerTarget :
NSObject

@property (nonatomic,weak)
id target;

@property (nonatomic,assign)
SEL selector;

- (void)timerDidFire:(NSTimer *)timer;

@end

@implementation LZLWeakTimerTarget

- (void)timerDidFire:(NSTimer *)timer {

if(_target) {

//消除arc警告

IMP imp = [_target
methodForSelector:_selector];

if ([NSStringFromSelector(_selector)
hasSuffix:@":"]) {

void (*func)(id,
SEL, id) = (void *)imp;

func(_target,
_selector, timer);

}else {

void (*func)(id,
SEL) = (void *)imp;

func(_target,
_selector);

}

} else {

[timer invalidate];

}

}

@end

@interface LZLTimer () {

NSTimer *_timer;

}

@end

@implementation LZLTimer

-(void)dealloc {

if (_timer!=nil) {

[_timer
invalidate];

_timer =
nil;

}

}

-(void)startTimerInterval:(NSTimeInterval)ti target:aTarget selector:(SEL)selector
userInfo:(id)userInfo repeats:(BOOL)yesOrNo {

if (nil ==
_timer) {

WMWeakTimerTarget *weakTarget = [[WMWeakTimerTarget
alloc] init];

weakTarget.target = aTarget;

weakTarget.selector = selector;

_timer = [NSTimer
scheduledTimerWithTimeInterval:ti
target:weakTarget selector:@selector(timerDidFire:)
userInfo:userInfo repeats:yesOrNo];

}

}

@end

NSTimer解除循环引用的更多相关文章

  1. iOS 里面 NSTimer 防止 循环引用

    使用NSTimer的类 #import "TBTimerTestObject.h" #import "TBWeakTimerTarget.h" @interfa ...

  2. NSTimer的循环引用

    在日常开发中想到会引起循环引用的一般比较容易想起的是 1.delegate 2.block 今天要说的就是另外一个,NSTimer 这个比较容易会被忽略掉 简单的说就是创建timer为成员变量的时候t ...

  3. CADisplayLink、NSTimer循环引用解决方案

    前言:CADisplayLink.NSTimer 循环引用问题 ​ CADisplayLink.NSTimer会对Target产生强引用,如果target又对他们产生强引用,那么就会引发循环引用. @ ...

  4. iOS 循环引用解决方案

    一.BLOCK 循环引用 一般表现为,某个类将block作为自己的属性变量,然后该类在block的方法体里面又使用了该类本身.构成循环引用. // 定义 block 的时候,会对外部变量做一次 cop ...

  5. 使用gc、objgraph干掉python内存泄露与循环引用!

    Python使用引用计数和垃圾回收来做内存管理,前面也写过一遍文章<Python内存优化>,介绍了在python中,如何profile内存使用情况,并做出相应的优化.本文介绍两个更致命的问 ...

  6. iOS 循环引用讲解(中)

    谈到循环引用,可能是delegate为啥非得用weak修饰,可能是block为啥要被特殊对待,你也可能仅仅想到了一个weakSelf,因为它能解决99%的关于循环引用的事情.下面我以个人的理解谈谈循环 ...

  7. ios block常见的错误(二)——循环引用

    这篇博文继续block的常见错误——循环引用. 循环引用是很多初学者不能察觉的,其产生的原因,是block中的代码会对对象进行强引用. 读者请阅读示例代码1,并思考示例代码1所创建的对象能否被正常销毁 ...

  8. 浅谈 关于ARC循环引用得问题

    这段时间在研究关于ARC得循环引用导致变量不能释放,在此先介绍一本书英文书: <Pro Multithreading and Memory Management for iOS and OS X ...

  9. iOS循环引用

    iOS循环引用 当前类的闭包/Block属性,用到了当前类,就会造成循环引用 此闭包/Block应该是当前类的属性,我们经常对Block进行copy,copy到堆中,以便后用. 单方向引用是不会产生循 ...

随机推荐

  1. Git:Git的安装过程

    Git:Git的安装过程 路径不要存在空格 默认即可,第一项为是否在页面显示 文本编辑器,默认VIM即可 设置环境变量: 1)最安全的选择,path环境变量不会改变,你只能在git bash里使用命令 ...

  2. POJ——T2186 Popular Cows || 洛谷——P2341 [HAOI2006]受欢迎的牛

    http://poj.org/problem?id=2186 || https://www.luogu.org/problem/show?pid=2341 Time Limit: 2000MS   M ...

  3. Java 获取环境变量

    Java 获取环境变量Java 获取环境变量的方式很简单: System.getEnv()  得到所有的环境变量System.getEnv(key) 得到某个环境变量的值 由于某些需要,可能要下载某些 ...

  4. 使用Modernizr检测支持CSS3

    使用Modernizr检测支持CSS3 如果支持某个属性,会增加一个class,名字就是该属性: 不支持,名字是no-某属性 还提供了一个全局Modernizr对象,使用如下: <script ...

  5. 排序(3)---------冒泡排序(C语言实现)

    说到冒泡排序,大一的时候第一次学习这个排序算法,可能大家不知道,"冒泡"在我说的方言里面是吹牛逼的意思. 所以就认为这个排序算法特吹牛逼有木有. 相信大家对全部的排序算法,这个想必 ...

  6. 从100PV到1亿级PV站点架构演变

    假设你对项目管理.系统架构有兴趣,请加微信订阅号"softjg".增加这个PM.架构师的大家庭 一个站点就像一个人,存在一个从小到大的过程. 养一个站点和养一个人一样.不同一时候期 ...

  7. m_Orchestrate learning system---九、在无法保证是否有图片的情况下,如何保证页面格式

    m_Orchestrate learning system---九.在无法保证是否有图片的情况下,如何保证页面格式 一.总结 一句话总结:都配上默认缩略图就可以解决了 1.如何获取页面get方式传过来 ...

  8. rem 、em

    介绍:rem是相对字体单位:根据html根元素大小而定,同样可作为宽高等单位: 适配原理:将px替换成rem,采用rem适配移动web的原理,根据不同屏幕宽度设置html的font-size的大小: ...

  9. Linux部署之批量自动安装系统之Kickstart篇

    1.         安装   2.         在桌面环境下啊配置   3.         Kickstart之基本配置   4.         Kickstart之安装方法   5.    ...

  10. 阿里云ecs : Couldn't connect to host, port: smtp.aliyun.com, 25; timeout -1;

    上传到服务器后javamail发邮件异常 链接 原来是ECS基于安全考虑,禁用了端口25. 改成465就可以发邮件了. p.setProperty("mail.smtp.socketFact ...