前言
在iOS中,NSTimer的使用是非常频繁的,但是NSTimer在使用中需要注意,避免循环引用的问题。之前经常这样写:
1- (void)setupTimer { 2 self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; 3} 4 5- (void)dealloc { 6 [self.timer invalidate]; 7 self.timer = nil; 8}
由于self强引用了timer,同时timer也强引用了self,所以循环引用造成dealloc方法根本不会走,self和timer都不会被释放,造成内存泄漏。
下面介绍一下几种解决timer循环引用的方法。
1. 选择合适的时机手动释放timer(该方法并不太合理)
在之前自己就是这样解决循环引用的:
-
控制器中
-
(void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated];
[self.timer invalidate]; self.timer = nil; }
-
-
view中
-
(void)removeFromSuperview { [super removeFromSuperview];
[self.timer invalidate]; self.timer = nil; }
-
在某些情况下,这种做法是可以解决问题的,但是有时却会引起其他问题,比如控制器push到下一个控制器,viewDidDisappear后,timer被释放,此时再回来,timer已经不复存在了。
所以,这种"方案"并不是合理的。
2. timer使用block方式添加Target-Action
这里我们需要自己在NSTimer的分类中添加类方法:
1@implementation NSTimer (BlcokTimer) 2 3+ (NSTimer *)bl_scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void (^)(void))block repeats:(BOOL)repeats { 4 5 return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(bl_blockSelector:) userInfo:[block copy] repeats:repeats]; 6} 7 8+ (void)bl_blockSelector:(NSTimer *)timer { 9 10 void(^block)(void) = timer.userInfo; 11 if (block) { 12 block(); 13 } 14} 15@end
通过block的方式,获取action,实际的target设置为self,即NSTimer类。这样我们在使用timer时,由于target的改变,就不再有循环引用了。 使用中还需要注意block可能引起的循环引用,所以使用weakSelf:
1__weak typeof(self) weakSelf = self; 2self.timer = [NSTimer bl_scheduledTimerWithTimeInterval:1 block:^{ 3 [weakSelf changeText]; 4} repeats:YES];
虽然没有了循环引用,但是还是应该记得在dealloc时释放timer。
3. 给self添加中间件proxy
考虑到循环引用的原因,改方案就是需要打破这些相互引用关系,因此添加一个中间件,弱引用self,同时timer引用了中间件,这样通过弱引用来解决了相互引用,如图:

接下来看看怎么实现这个中间件,直接上代码:
1@interface ZYWeakObject() 2 3@property (weak, nonatomic) id weakObject; 4 5@end 6 7@implementation ZYWeakObject 8 9- (instancetype)initWithWeakObject:(id)obj { 10 _weakObject = obj; 11 return self; 12} 13 14+ (instancetype)proxyWithWeakObject:(id)obj { 15 return [[ZYWeakObject alloc] initWithWeakObject:obj]; 16} 17 18 19@interface ZYWeakObject() 20 21@property (weak, nonatomic) id weakObject; 22 23@end 24 25@implementation ZYWeakObject 26 27- (instancetype)initWithWeakObject:(id)obj { 28 _weakObject = obj; 29 return self; 30} 31 32+ (instancetype)proxyWithWeakObject:(id)obj { 33 return [[ZYWeakObject alloc] initWithWeakObject:obj]; 34} 35 36仅仅添加了weak类型的属性还不够,为了保证中间件能够响应外部self的事件,需要通过消息转发机制,让实际的响应target还是外部self,这一步至关重要,主要涉及到runtime的消息机制。 37 38/** 39 * 消息转发,让_weakObject响应事件 40 */ 41- (id)forwardingTargetForSelector:(SEL)aSelector { 42 return _weakObject; 43} 44 45- (void)forwardInvocation:(NSInvocation *)invocation { 46 void *null = NULL; 47 [invocation setReturnValue:&null]; 48} 49 50- (BOOL)respondsToSelector:(SEL)aSelector { 51 return [_weakObject respondsToSelector:aSelector]; 52} 53 54
接下来就可以这样使用中间件了:
1// target要设置成weakObj,实际响应事件的是self 2ZYWeakObject *weakObj = [ZYWeakObject proxyWithWeakObject:self]; 3self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:weakObj selector:@selector(changeText) userInfo:nil repeats:YES];
结论
经测试,以上两种方案都是可以解决timer的循环引用问题
代码请移步github: Demo https://github.com/zhouyangyng/timerRetainCycle