使用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 防止 循环引用的更多相关文章

  1. iOS 容易引“起循环引用”的三种场景

    笔者在阅读中总结了一下,在iOS平台容易引起循环引用的四个场景: 一.parent-child相互持有.委托模式 [案例]:   @interface FTAppCenterMainViewContr ...

  2. NSTimer的循环引用

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

  3. NSTimer解除循环引用

    NSTimer作为一个经常使用的类,却有一个最大的弊病,就是会强引用target.造成调用timer很麻烦.稍有不慎就造成内存泄漏. 下面就是为解决问题做的封装. 直接上代码: #import < ...

  4. ios block 导致的循环引用

    [[NSNotificationCenter defaultCenter] addObserverForName:@"UIWindowDidRotateNotification" ...

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

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

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

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

  7. iOS之weak和strong、懒加载及循环引用

    一.weak和strong 1.理解 刚开始学UI的时候,对于weak和strong的描述看得最多的就是“由ARC引入,weak相当于OC中的assign,但是weak用于修饰对象,但是他们都不会造成 ...

  8. 【转】iOS学习之容易造成循环引用的三种场景

    ARC已经出来很久了,自动释放内存的确很方便,但是并非绝对安全绝对不会产生内存泄露.导致iOS对象无法按预期释放的一个无形杀手是——循环引用.循环引用可以简单理解为A引用了B,而B又引用了A,双方都同 ...

  9. 【原】iOS容易造成循环引用的三种场景,就在你我身边!

    ARC已经出来很久了,自动释放内存的确很方便,但是并非绝对安全绝对不会产生内存泄露.导致iOS对象无法按预期释放的一个无形杀手是——循环引用.循环引用可以简单理解为A引用了B,而B又引用了A,双方都同 ...

随机推荐

  1. css之px自动转rem—“懒人”必备

    作为一名前端开发,尤其是在做移动端适配时,rem是我们经常用到的单位,它的好处大家可以自行搜索,网上已经有很多了.但是我们再将设计稿上的px转换成rem时,得手动的去计算,这是一个很耗时.费力的过程, ...

  2. new与malloc

    首先将new与malloc的区别总结如下: 1 new可以自动计算需要分配多大的内存,而malloc必须指明. 2 new返回的指针是有类型的,malloc返回void*类型的指针. 3 new在分配 ...

  3. Java常用的7大排序算法汇总

    1.插入排序算法 插入排序的基本思想是在遍历数组的过程中,假设在序号 i 之前的元素即 [0..i-1] 都已经排好序,本趟需要找到 i 对应的元素 x 的正确位置 k ,并且在寻找这个位置 k 的过 ...

  4. runtime 运行机制2

    Mike_zh QQ:82643885 end: blogTitle 博客的标题和副标题 博客园 首页 新随笔 联系 订阅 <a id="MyLinks1_XMLLink" ...

  5. myeclipse,eclipse打开当前文件所在文件夹

    方法一: eclipse打开当前文件所在文件夹的插件Run-->External Tools-->External Tools Configurations...new 一个 progra ...

  6. EasyUI DataGrid getChecked/getSelections 获取不到数据

    今天使用getChecked获取选择的行,结果总是获取一行数据,于是换用getSelections,结果还是一样,想起之前做的项目,把idField换了下,之后getChecked/getSelect ...

  7. ServiceStack.OrmLite中的一些"陷阱"(1)

    使用过ServiceStack.Ormlite的人都应该知道,其作为一个轻量级的ORM,使用的便捷度非常高,用起来就一个字:爽!而支撑其便捷度的,是库内大量地使用了扩展方法及静态变量. 首先先从源头入 ...

  8. 硬浮点 VFP

    http://blog.chinaunix.net/uid-27875-id-3449290.html   编译器对VFP的支持一个浮点数操作最后是翻译成VFP指令,还是翻译成fpa,或者是softf ...

  9. css3中的颜色

    1颜色.color:rgba(R,G,B,A) R,G,B是分别代笔红,绿,蓝值是在0到255之间的数也可以是0.0% - 100.0%,A代表的是透明度0到1之间. 2.渐变.background- ...

  10. NOIP2014 总结

    想了很久,才开始动笔. 怎么说,感觉挺对不起自己的.愚蠢的失误让我正好卡着一等线,真希望不要是二等奖. 最难过的是,努力全葬送在愚蠢上面了. 不过也好,学会平静自己也是一种能力. 半期考试也遭的一塌糊 ...