NSProxy解决NSTimer和CADisplayLink的循

2018-09-06  本文已影响8人  霸_霸霸

NSTimer和CADisplayLink都需要添加到Runloop才能正常运作, 但是都会引起循环引用

NSProxy解决循环引用

上图表明了循环引用的原因以及使用NSProxy解决循环引用的原理

解决方案:

1. 一劳永逸型NSProxy

NSProxy是类似于Runtime中的消息转发, 原理就是将Proxy收到的消息转发给target执行, 从而避免循环引用, 且NSProxy可以同时适用于NSTimerCADisplayLink.
NSProxy主要是两个方法

//1. 获取方法签名
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel;
//2. 类似于方法重定向
- (void)forwardInvocation:(NSInvocation *)invocation;

1.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;
}
  1. 创建一个属性target, 用来接收传被NSTimer或者CADisplayLink强引的对象, 注意, 这里要用weak关键字
@interface TargetProxy()

//target, 这里必须要用weak, 因为某个对象会强引TimeProxy对象, TimeProxy对象不能再强引target, 否则会形成循环引用
@property (nonatomic, weak)id target;

@end
  1. 实现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工具类

_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:[TargetProxy proxyWithTarget:self] selector:@selector(yourMethod) userInfo:nil repeats:YES];

别忘了在dealloc中调用[self.timer invalidate];

_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的方式, 就不会造成循环引用

__weak typeof(self) weakSelf = self;
_timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:weakSelf selector:@selector(timerMethod) userInfo:nil repeats:YES];
[self.timer fire];
__weak typeof(self) weakSelf = self;
_timer = [NSTimer scheduledTimerWithTimeInterval:0.2 repeats:YES block:^(NSTimer * _Nonnull timer) {
    [weakSelf timerMethod];
}];
[self.timer fire];

GitHub源码

上一篇 下一篇

猜你喜欢

热点阅读