CADisplayLink 与NSTimer 循环引用问题 ,NSTimer准时吗?

由下面代码可以看出   CADisplayLink 与NSTimer 中的targert 会发生强引用的关系,如何解决呐?

@interface ViewController ()

@property(nonatomic,strong)CADisplayLink *link;
@property(nonatomic,strong)NSTimer *timer;
@end

@implementation ViewController


- (void)test3{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self      selector:@selector(timerTest) userInfo:nil repeats:YES];
}
- (void)timerTest{
    
    NSLog(@"%s",__func__);
}

- (void)test2{
    
    self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(linkTest)];
    [self.link addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];
    
}

- (void)linkTest{
    NSLog(@"%s",__func__);
}

- (void)dealloc{
    [self.link invalidate];
    [self.timer invalidate];
}
@end

解决NStimer 的强引用方法一:

使用timer的block 方法加弱指针,__weak typeof(self)weakSelf = self;
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [weakSelf timer];
    }];

方法二:

代码如下:利用了消息转发机制

@interface XZProxy : NSObject
@property(nonatomic,weak) id target;

+ (instancetype)proxyWithTarget:(id) target; 
@end

#import "XZProxy.h"

@implementation XZProxy
+ (instancetype)proxyWithTarget:(id)target{
    XZProxy *p = [[XZProxy alloc]init];
    p.target = target;
    return p;
}
@end


//控制器中方法的调用为
 self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:[XZProxy proxyWithTarget:self] selector:@selector(timerTest) userInfo:nil repeats:YES];

//同样CADisplaylink 也可以通过这种方法解决
 self.link = [CADisplayLink displayLinkWithTarget:[XZProxy proxyWithTarget:self] selector:@selector(linkTest)];

另外一种比较高效的方法就是使用NSProxy,他的高效在于:直接进入消息转发,不需要到父类等去查找方法。效率较高


@interface XZProxy1 : NSProxy
@property(nonatomic,weak) id target;

+ (instancetype)proxyWithTarget:(id) target;

@end


#import "XZProxy1.h"

@implementation XZProxy1

+ (instancetype)proxyWithTarget:(id) target{
    //NSProxy 本身没有init的方法,直接调用alloc 分配内存空间
    XZProxy1 *p2 = [XZProxy1 alloc];
    p2.target = target;
    return p2;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel{
    return [self.target methodSignatureForSelector:sel];
}
- (void)forwardInvocation:(NSInvocation *)invocation{
    [invocation invokeWithTarget:self.target];
}
@end

NSTimer 准时吗?

答案是:不准时。NSTimer 执行任务是依赖runloop的,runloop 在一定时间段是有很多任务需要处理,只能在恰当的时候处理NSTimer的任务,加入在一段时间内,runloop的任务比较繁重,会导致NStimer的任务延时,所以他不准时。

但是我们可以使用GCD的定时器来代替NStimer,GCD的内部的定时器是与系统内核直接挂钩的,而且他不依赖runloop,所以很准时。

猜你喜欢

转载自blog.csdn.net/qq_33726122/article/details/84788476