问题简介
在iOS中,NSTimer的使用非常频繁,但是NSTimer在使用中需要注意,避免循环引用的问题:
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
- (void)dealloc {
[self.timer invalidate];
self.timer = nil;
}
由于self强引用了timer,同时timer也强引用了self,所以循环引用self和timer都不会释放,造成内存泄漏。
一、提前手动释放timer
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self.timer invalidate];
self.timer = nil;
}
二、timer使用block方式添加Target-Action
需要自己在NSTimer的分类中添加类方法:
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void (^)(void))block repeats:(BOOL)repeats {
return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(blockSelector:) userInfo:[block copy] repeats:repeats];
}
+ (void)blockSelector:(NSTimer *)timer {
void(^block)(void) = timer.userInfo;
if (block) {
block();
}
}
注意:在dealloc时,应该释放timer
三、给self添加中间件proxy
weak指针问题:
用__weak修饰self,做法是无效的。因为无论是weak还是strong修饰,在NSTimer中都会重新生成一个新的强引用指针指向self,导致循环引用的。
原理跟类方法相似,打破循环引用的环路。将timer的target设置为WeakProxy实例,利用消息转发机制实现执行VC中的计时方法,解决循环引用。
创建一个继承NSObject的子类WeakObject,并创建开启计时器的方法。
初始化中间件对象
@interface WeakObject : NSObject
- (instancetype)initWithWeakObject:(id)obj;
+ (instancetype)proxyWithWeakObject:(id)obj;
@end
实现方法
@interface WeakObject()
@property (weak, nonatomic) id weakObject;
@end
@implementation WeakObject
- (instancetype)initWithWeakObject:(id)obj {
_weakObject = obj;
return self;
}
+ (instancetype)proxyWithWeakObject:(id)obj {
return [[WeakObject 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];
}
/// 返回方法的签名
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector {
return [NSObject instanceMethodSignatureForSelector:@selector(init)];
}
对象的调用
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:[ZYWeakObject proxyWithWeakObject:self] selector:@selector(timerAction) userInfo:nil repeats:YES];
网友评论