前言
在iOS中,NSTimer的使用是非常频繁的,但是NSTimer在使用中需要注意,避免循环引用的问题。之前经常这样写:
- (void)setupTimer {
self.timer= [NSTimer scheduledTimerWithTimeInterval:1target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
}- (void)dealloc {
[self.timer invalidate];
self.timer=nil;
}
由于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的分类中添加类方法:
@implementationNSTimer (BlcokTimer)+ (NSTimer *)bl_scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void (^)(void))block repeats:(BOOL)repeats {return[self scheduledTimerWithTimeInterval:interval target:self selector:@selector(bl_blockSelector:) userInfo:[block copy] repeats:repeats];
}+ (void)bl_blockSelector:(NSTimer *)timer {void(^block)(void) =timer.userInfo;if(block) {
block();
}
}@end
通过block的方式,获取action,实际的target设置为self,即NSTimer类。这样我们在使用timer时,由于target的改变,就不再有循环引用了。 使用中还需要注意block可能引起的循环引用,所以使用weakSelf:
__weak typeof(self) weakSelf =self;
self.timer= [NSTimer bl_scheduledTimerWithTimeInterval:1 block:^{
[weakSelf changeText];
} repeats:YES];
虽然没有了循环引用,但是还是应该记得在dealloc时释放timer。
3. 给self添加中间件proxy
考虑到循环引用的原因,改方案就是需要打破这些相互引用关系,因此添加一个中间件,弱引用self,同时timer引用了中间件,这样通过弱引用来解决了相互引用,如图:
接下来看看怎么实现这个中间件,直接上代码:
@interface ZYWeakObject()
@property (weak, nonatomic) id weakObject;
@end
@implementation ZYWeakObject
- (instancetype)initWithWeakObject:(id)obj {
_weakObject = obj;
return self;
}
+ (instancetype)proxyWithWeakObject:(id)obj {
return [[ZYWeakObject alloc] initWithWeakObject:obj];
}
@interfaceZYWeakObject()
@property (weak, nonatomic)idweakObject;@end
@implementationZYWeakObject- (instancetype)initWithWeakObject:(id)obj {
_weakObject=obj;returnself;
}+ (instancetype)proxyWithWeakObject:(id)obj {return[[ZYWeakObject alloc] initWithWeakObject:obj];
}
仅仅添加了weak类型的属性还不够,为了保证中间件能够响应外部self的事件,需要通过消息转发机制,让实际的响应target还是外部self,这一步至关重要,主要涉及到runtime的消息机制。
/**
* 消息转发,让_weakObject响应事件*/
- (id)forwardingTargetForSelector:(SEL)aSelector {return_weakObject;
}- (void)forwardInvocation:(NSInvocation *)invocation {void *null =NULL;
[invocation setReturnValue:&null];
}-(BOOL)respondsToSelector:(SEL)aSelector {return[_weakObject respondsToSelector:aSelector];
}
接下来就可以这样使用中间件了:
//target要设置成weakObj,实际响应事件的是self
ZYWeakObject *weakObj =[ZYWeakObject proxyWithWeakObject:self];
self.timer= [NSTimer scheduledTimerWithTimeInterval:1 target:weakObj selector:@selector(changeText) userInfo:nil repeats:YES];
结论
经测试,以上两种方案都是可以解决timer的循环引用问题