NSTimer解除循环引用
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解除循环引用的更多相关文章
- iOS 里面 NSTimer 防止 循环引用
使用NSTimer的类 #import "TBTimerTestObject.h" #import "TBWeakTimerTarget.h" @interfa ...
- NSTimer的循环引用
在日常开发中想到会引起循环引用的一般比较容易想起的是 1.delegate 2.block 今天要说的就是另外一个,NSTimer 这个比较容易会被忽略掉 简单的说就是创建timer为成员变量的时候t ...
- CADisplayLink、NSTimer循环引用解决方案
前言:CADisplayLink.NSTimer 循环引用问题 CADisplayLink.NSTimer会对Target产生强引用,如果target又对他们产生强引用,那么就会引发循环引用. @ ...
- iOS 循环引用解决方案
一.BLOCK 循环引用 一般表现为,某个类将block作为自己的属性变量,然后该类在block的方法体里面又使用了该类本身.构成循环引用. // 定义 block 的时候,会对外部变量做一次 cop ...
- 使用gc、objgraph干掉python内存泄露与循环引用!
Python使用引用计数和垃圾回收来做内存管理,前面也写过一遍文章<Python内存优化>,介绍了在python中,如何profile内存使用情况,并做出相应的优化.本文介绍两个更致命的问 ...
- iOS 循环引用讲解(中)
谈到循环引用,可能是delegate为啥非得用weak修饰,可能是block为啥要被特殊对待,你也可能仅仅想到了一个weakSelf,因为它能解决99%的关于循环引用的事情.下面我以个人的理解谈谈循环 ...
- ios block常见的错误(二)——循环引用
这篇博文继续block的常见错误——循环引用. 循环引用是很多初学者不能察觉的,其产生的原因,是block中的代码会对对象进行强引用. 读者请阅读示例代码1,并思考示例代码1所创建的对象能否被正常销毁 ...
- 浅谈 关于ARC循环引用得问题
这段时间在研究关于ARC得循环引用导致变量不能释放,在此先介绍一本书英文书: <Pro Multithreading and Memory Management for iOS and OS X ...
- iOS循环引用
iOS循环引用 当前类的闭包/Block属性,用到了当前类,就会造成循环引用 此闭包/Block应该是当前类的属性,我们经常对Block进行copy,copy到堆中,以便后用. 单方向引用是不会产生循 ...
随机推荐
- PHP读取XML数据中CDATA内数值
// 在开发过程中遇到对XML获取时候加载 CDATA 无法读取内部的数值(例如微信平台的返回值) $content = simplexml_load_string('<content>& ...
- url中jsessionid的理解
(1) 这是一个保险措施 因为Session默认是需要Cookie支持的 但有些客户浏览器是关闭Cookie的 这个时候就需要在URL中指定服务器上的session标识,也就是5F4771183629 ...
- [React] Use the new React Context API
The React documentation has been warning us for a long time now that context shouldn't be used and t ...
- bzoj-1492 货币兑换Cash (1)——平衡树维护凸包
题意: 有n天和m的初始金钱,用来购买AB两种纪念券: n天里每天都有AB的价格.每天能够进行这种操作. 1.卖出手中x%的纪念券(AB分别都卖出x%). 2.用x的金钱买入纪念券.买入AB券的比例在 ...
- php扩展之 pdo_mysql.so
总结:新搭编译安装的 nginx+php+mysql环境,执行之前开发的项目遇到了没有安装pdo的问题 1.进入到php5的源代码包里面,ext以下.找到pdo_mysql目录 首先运行:/usr/l ...
- Linux就该这么学 20181005(第九章SSH远程对话)
参考链接https://www.linuxprobe.com/ nmtui开启网卡设置 ONBOOT=yes systemctl restart network nmcli connection sh ...
- 18.boost 图的拓扑排序
运行结果: 代码示例: #include <iostream> #include <vector> #include <deque> #include <bo ...
- c# iTextSharp导出PDF
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Th ...
- 常用css框架 Sass/Less
Bootstrap less/sass Sass (Syntactically Awesome Stylesheets)是一种动态样式语言,Sass语法属于缩排语法,比css比多出好些功能(如变量.嵌 ...
- 51nod 1101 换零钱 完全背包的变型 动态规划
题目: 思路: ;i < ; i++){ for(int j = a[i];j <= n; j++){ dp[j] = (dp[j] + dp[j-a[i]])%mod; } } a[i] ...