版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1336807
循环引用问题:
将一个NSTimer对象作为一个控制器的的属性,这时当前VC对NSTimer对象进行了一次强引用。在创建NSTimer兑现的时候,NSTimer对象又将当前VC作为自己的target,这时NSTimer对象对当前VC进行了一次强引用,这样就造成了NSTimer和当前VC的循环引用,从而让VC和NSTimer都无法释放,最终导致内存泄漏。
通常代码:
我们可以为NSTimer创建一个分类,在分类中添加一个创建NSTimer对象的方法,以避免循环引用的问题。
NSTimer分类代码:
.m中的代码
可复制代码:
}
}
使用代码:
可复制代码:
weakifySelf self.timer = [NSTimer resolve_scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer *timer) { strongifySelf self doThings; NSLog(@"----------------"); }];
关于weakSelf和strongSelf:
__weak __typeof(&*self)weakSelf = self;
__strong __typeof(&*weakSelf)self = weakSelf;
demo地址:https://gitee.com/liangsenliangsen/nstimer_loop_reference.git
本篇文章到这里就结束了,愿大家加班不多工资多,男同胞都有女朋友,女同胞都有男朋友。