NSTimer和CADisplayLink都需要添加到Runloop
才能正常运作, 但是都会引起循环引用
NSProxy解决循环引用
上图表明了循环引用的原因以及使用NSProxy
解决循环引用的原理
解决方案:
1. 一劳永逸型NSProxy
NSProxy
是类似于Runtime
中的消息转发, 原理就是将Proxy收到的消息转发给target执行, 从而避免循环引用, 且NSProxy
可以同时适用于NSTimer
和CADisplayLink
.
NSProxy
主要是两个方法
//1. 获取方法签名
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel;
//2. 类似于方法重定向
- (void)forwardInvocation:(NSInvocation *)invocation;
1.1 代码:
- 初始化
//.h文件
#import <Foundation/Foundation.h>
@interface TargetProxy : NSProxy
+ (instancetype)proxyWithTarget:(id)target;
@end
---------------------------------------------
//.m文件
+ (instancetype)proxyWithTarget:(id)target {
TargetProxy *proxy = [[self class] alloc];
proxy.target = target;
return proxy;
}
- 创建一个属性target, 用来接收传被
NSTimer
或者CADisplayLink
强引的对象, 注意, 这里要用weak关键字
@interface TargetProxy()
//target, 这里必须要用weak, 因为某个对象会强引TimeProxy对象, TimeProxy对象不能再强引target, 否则会形成循环引用
@property (nonatomic, weak)id target;
@end
- 实现NSProxy的两个方法
//获取target类中的sel方法的方法签名
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel {
return [self.target methodSignatureForSelector:sel];
}
- (void)forwardInvocation:(NSInvocation *)invocation {
//判断target是否实现了该方法
if ([self.target respondsToSelector:invocation.selector]) {
//让target调用该方法
[invocation invokeWithTarget:self.target];
}else {
//找不到该方法
[invocation doesNotRecognizeSelector:invocation.selector];
}
}
1.2 如何调用NSProxy工具类
- NSTimer
_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:[TargetProxy proxyWithTarget:self] selector:@selector(yourMethod) userInfo:nil repeats:YES];
别忘了在dealloc
中调用[self.timer invalidate];
- CADisplayLink
_link = [CADisplayLink displayLinkWithTarget:[TargetProxy proxyWithTarget:self] selector:@selector(linkMethod:)];
[self.link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
同样, 需要在dealloc
中调用[self.link invalidate];
2. 针对NSTimer
针对NSTimer, 我们可以采用block + weakSelf
的方式替代target
的方式, 就不会造成循环引用
-
target
的方式会造成循环引用, 可用NSProxy工具类解决
__weak typeof(self) weakSelf = self;
_timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:weakSelf selector:@selector(timerMethod) userInfo:nil repeats:YES];
[self.timer fire];
-
block + weakSelf
的方式不会造成循环引用
__weak typeof(self) weakSelf = self;
_timer = [NSTimer scheduledTimerWithTimeInterval:0.2 repeats:YES block:^(NSTimer * _Nonnull timer) {
[weakSelf timerMethod];
}];
[self.timer fire];