iOS 里面 NSTimer 防止 循环引用
使用NSTimer的类
#import "TBTimerTestObject.h"
#import "TBWeakTimerTarget.h" @interface TBTimerTestObject() @property (nonatomic, weak) NSTimer *timer; @end @implementation TBTimerTestObject - (void)dealloc
{
NSLog(@"timer is dealloc");
} - (id)init
{
self = [super init]; if (self) {
TBWeakTimerTarget *timerTarget =[[TBWeakTimerTarget alloc] initWithTarget:self andSelector:@selector(timerDidFire:)]; _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:timerTarget selector:@selector(timerDidFire:) userInfo:nil repeats:YES];
}
return self;
} - (void)timerDidFire:(NSTimer*)timer
{
NSLog(@"%@", @"");
} @end
target类:
头文件:
#import <Foundation/Foundation.h> @interface TBWeakTimerTarget : NSObject - (instancetype) initWithTarget: (id)target andSelector:(SEL) selector;
- (void)timerDidFire:(NSTimer *)timer; @end
m文件:
#import "TBWeakTimerTarget.h" @implementation TBWeakTimerTarget
{
__weak id _target;
SEL _selector;
} - (instancetype) initWithTarget: (id) target andSelector: (SEL) selector
{
self = [super init];
if (self) {
_target = target;
_selector = selector;
}
return self;
} - (void) dealloc
{
NSLog(@"TBWeakTimerTarget dealloc");
} - (void)timerDidFire:(NSTimer *)timer
{
if(_target)
{
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
[_target performSelector:_selector withObject:timer];
#pragma clang diagnostic pop
}
else
{
[timer invalidate];
}
}
@end
使用示范:
@interface ViewController () @property (nonatomic, strong) TBTimerTestObject *obj; @end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
} - (IBAction)tap:(id)sender
{
NSLog(@"tappp"); self.obj = [[TBTimerTestObject alloc] init]; [self performSelector:@selector(timerDidFired:) withObject:nil afterDelay:];
} - (void)timerDidFired:(NSTimer *)timer
{
self.obj = nil;
NSLog(@"AppDelegate timerDidFired");
} @end
打印log:
-- ::40.120 XIBTest[:] tappp
-- ::41.121 XIBTest[:]
-- ::42.121 XIBTest[:]
-- ::43.121 XIBTest[:]
-- ::43.122 XIBTest[:] timer is dealloc
-- ::43.122 XIBTest[:] AppDelegate timerDidFired
-- ::44.121 XIBTest[:] TBWeakTimerTarget dealloc
具体参考了:
http://stackoverflow.com/questions/16821736/weak-reference-to-nstimer-target-to-prevent-retain-cycle
国内某些说在dealloc中写:
timer.invalid;
timer = nil;
是不行的,因为循环引用了,都不会进入dealloc方法中。具体可以自己试试。
iOS 里面 NSTimer 防止 循环引用的更多相关文章
- iOS 容易引“起循环引用”的三种场景
笔者在阅读中总结了一下,在iOS平台容易引起循环引用的四个场景: 一.parent-child相互持有.委托模式 [案例]: @interface FTAppCenterMainViewContr ...
- NSTimer的循环引用
在日常开发中想到会引起循环引用的一般比较容易想起的是 1.delegate 2.block 今天要说的就是另外一个,NSTimer 这个比较容易会被忽略掉 简单的说就是创建timer为成员变量的时候t ...
- NSTimer解除循环引用
NSTimer作为一个经常使用的类,却有一个最大的弊病,就是会强引用target.造成调用timer很麻烦.稍有不慎就造成内存泄漏. 下面就是为解决问题做的封装. 直接上代码: #import < ...
- ios block 导致的循环引用
[[NSNotificationCenter defaultCenter] addObserverForName:@"UIWindowDidRotateNotification" ...
- iOS 循环引用讲解(中)
谈到循环引用,可能是delegate为啥非得用weak修饰,可能是block为啥要被特殊对待,你也可能仅仅想到了一个weakSelf,因为它能解决99%的关于循环引用的事情.下面我以个人的理解谈谈循环 ...
- CADisplayLink、NSTimer循环引用解决方案
前言:CADisplayLink.NSTimer 循环引用问题 CADisplayLink.NSTimer会对Target产生强引用,如果target又对他们产生强引用,那么就会引发循环引用. @ ...
- iOS之weak和strong、懒加载及循环引用
一.weak和strong 1.理解 刚开始学UI的时候,对于weak和strong的描述看得最多的就是“由ARC引入,weak相当于OC中的assign,但是weak用于修饰对象,但是他们都不会造成 ...
- 【转】iOS学习之容易造成循环引用的三种场景
ARC已经出来很久了,自动释放内存的确很方便,但是并非绝对安全绝对不会产生内存泄露.导致iOS对象无法按预期释放的一个无形杀手是——循环引用.循环引用可以简单理解为A引用了B,而B又引用了A,双方都同 ...
- 【原】iOS容易造成循环引用的三种场景,就在你我身边!
ARC已经出来很久了,自动释放内存的确很方便,但是并非绝对安全绝对不会产生内存泄露.导致iOS对象无法按预期释放的一个无形杀手是——循环引用.循环引用可以简单理解为A引用了B,而B又引用了A,双方都同 ...
随机推荐
- JS学习之函数内部属性和方法
知识点:arguments和this对象.caller属性.apply()和call()方法 arguments对象:函数内部对象,传入函数中所有参数的集合,类数组对象 属性:callee 指 ...
- python-->基础-->005-->类的三大成员:方法+属性+字段
---恢复内容开始--- 一.方法 python的方法中分为三种方法: 静态方法 动态方法(普通方法) 类方法 其中常用的方法为:静态方法和动态方法 class MyClass: def __init ...
- LVM增大和减小ext4、xfs分区
可以对ext4调整分区大小,能自动识别要增大还是减小 lvresize -L 300M -r /dev/vg/lvol0 原文地址http://www.361way.com/lvm-xfs-ext4/ ...
- angular报$injector / unpr的错误
原因:angular引用未定义的错误,因为JS代码压缩过后找不到申明的变量,,没在使用之前定义,且代码被压缩,则报错(变量压缩后会变成单个字母无法识别,需在引用前分别定义): 解决:angular.m ...
- python splinter
from splinter.browser import Browser with Browser() as b: for url,name in web: b.visit(url) b.fill(' ...
- linux环境下jdk的安装步骤
JDK的安装步骤:1. 把jdk文件cp到服务器上2. 加权限 chmod +x 文件3. 执行 ./4.修改配置 vi /etc/profile 最后一行添加:export JAVA_HOME=/ ...
- rdlc报表大小设置
参考:http://stackoverflow.com/questions/427730/how-to-limit-rdlc-report-for-one-page-in-a-pdf 主要设置为:报表 ...
- win8 vs2010 openni2 配置
打开一个新项目或者已存在的项目用以使用 OpenNI 在Visual Studio 菜单中, 打开项目菜单,选择项目属性. 在C/C++ 选项卡中, 在"常规" 下, 选择 &q ...
- My English Dictionary
A axis 坐标轴 architecture 结构 B C consider 考虑 closure 闭包 clip 修剪 convert 改变 D default 默认的 valid 有效的 d ...
- [学习笔记]坚果云网盘,SVN异地代码管理
SVN的好处不必多说了.但是如果希望有一份自己的用来学习和储备的代码仓库,那么能够异地同步是必不可少的了. 参考作者Mike_QSJ的文章,但是实际上做了很大的改动.一方面使用更常见的windows系 ...