美文网首页
内存管理-定时器(CADisplayLink、NSTimer)

内存管理-定时器(CADisplayLink、NSTimer)

作者: 依米米一 | 来源:发表于2021-05-26 17:21 被阅读0次

    使用CADisplayLink、NSTimer有什么需要注意的?

    一、创建定时器

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

    CADisplayLink需要加到RunLoop中才能使用

     self.link = [CADisplayLink displayLinkWithTarget:self selector:@selector(linktest)];
     [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    
    - (void)linktest{
        NSLog(@"%s", __func__);
    }
    - (void)dealloc
    {
        [self.link invalidate];
        NSLog(@"%s", __func__);
    }
    

    运行结果:一直在调用,即使页面摧毁也在调用

    2021-05-26 16:19:13.167971+0800 Interview03-定时器[32664:4448900] -[ViewController linktest]
    2021-05-26 16:19:13.184755+0800 Interview03-定时器[32664:4448900] -[ViewController linktest]
    2021-05-26 16:19:13.201062+0800 Interview03-定时器[32664:4448900] -[ViewController linktest]
    2021-05-26 16:19:13.217795+0800 Interview03-定时器[32664:4448900] -[ViewController linktest]
    2021-05-26 16:19:13.234851+0800 Interview03-定时器[32664:4448900] -[ViewController linktest]
    

    小结:造成循环引用

    原因:self对CADisplayLink强引用,CADisplayLink对target强引用,target对self强引用,造成循环引用,NSTimer也有类似问题
    NSTimer的scheduledTimerWithTimeInterval创建方式会造成循环引用

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

    二、问题抛出

    NSTimer的这种scheduledTimerWithTimeInterval创建方式以及CADisplayLink的displayLinkWithTarget创建方式易造成循环引用

    三、解决办法

    1、尝试_ _weak

      __weak typeof (self) weakself = self;
     self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:weakself selector:@selector(timerTest) userInfo:nil repeats:YES];
    

    循环引用问题仍旧存在

    分析原因:
    往常的能解决是因为在block中 此处传参给taget 传进去的都是指针 强弱引用都没区别

    2、使用block 弱引用

    __weak typeof (self) weakself = self;
     self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 repeats:YES block:^(NSTimer * _Nonnull timer) {
     [weakself timerTest];
     }];
    

    self对NSTimer强引用,NSTimer对block强引用 block对self产生弱引用weakself

    3、使用代理对象

    引一个弱引用

    3.1继承NSObject
    .h文件中

    @interface MJProxy1 : NSObject
    + (instancetype)proxyWithTarget:(id)target;
    @property (weak, nonatomic) id target;//弱引用
    @end
    

    .m文件中

    @implementation MJProxy1
    
    + (instancetype)proxyWithTarget:(id)target
    {
        MJProxy1 *proxy = [[MJProxy1 alloc] init];
        proxy.target = target;
        return proxy;
    }
    
    //消息转发三种
    - (id)forwardingTargetForSelector:(SEL)aSelector
    {
        return self.target;
    }
    

    调用

     self.link = [CADisplayLink displayLinkWithTarget:[MJProxy1 proxyWithTarget:self] selector:@selector(linktest)];
    [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:[MJProxy1 proxyWithTarget:self] selector:@selector(timerTest) userInfo:nil repeats:YES];
    

    不再循环引用
    3.1继承NSProxy
    .h文件中

    @interface MJProxy : NSProxy
    + (instancetype)proxyWithTarget:(id)target;
    @property (weak, nonatomic) id target;
    @end
    

    .m文件中

    @implementation MJProxy
    
    + (instancetype)proxyWithTarget:(id)target
    {
        // NSProxy对象不需要调用init,因为它本来就没有init方法
        MJProxy *proxy = [MJProxy alloc]; 
        proxy.target = target;
        return proxy;
    }
    
    //消息转发
    //只要调用MJProxy的某个方法就马上调用他的另一个方法methodSignatureForSelector
    - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
    {
        return [self.target methodSignatureForSelector:sel];
    }
    
    
    //消息转发三种 调用不到时先调用这个
    - (void)forwardInvocation:(NSInvocation *)invocation
    {
        [invocation invokeWithTarget:self.target];
    }
    //- (id)forwardingTargetForSelector:(SEL)aSelector  NSProxy没有这个方法
    

    调用

     self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:[MJProxy proxyWithTarget:self] selector:@selector(timerTest) userInfo:nil repeats:YES];
    
    self.link = [CADisplayLink displayLinkWithTarget:[MJProxy proxyWithTarget:self] selector:@selector(linktest)];
    [self.link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
    

    同样问题解决,不再循环引用

    三、总结

    1、解决问题的宗旨,使用代理对象将强引用转为若引用,并用消息转发机制对找不到响应的方法进行转发
    2、 NSProxy 、NSObject 同为基类,不同点: NSProxy方法不存在时专门用来消息转发直接在自己类里搜索节省时间NSObject多了一步从父类里搜索方法的过程
    3、定时器依赖于runloop去实现的,有可能并不准时,如果定时器是设置每隔0.5秒,在runloop中 跑完一圈去看一次定时器到没有到0.5,到了就执行定时器方法,不到会继续跑圈执行别的任务,但每次循环时间不确定,有可能一圈任务多的时候超过0.5间隔时间导致不准确,所以项目中想用定时器最好用GCD,基于内核比较准时

    gcd简单的定时器

    @property(nonatomic,strong)dispatch_source_t gcdTimer;
    //0.创建一个队列
        dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
        //1.创建一个GCD的定时器
        /*
         第一个参数:说明这是一个定时器
         第四个参数:GCD的回调任务添加到那个队列中执行,如果是主队列则在主线程执行
         */
        dispatch_source_t gcdTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
        //2.设置定时器的开始时间,间隔时间以及精准度
        //设置开始时间,三秒钟之后调用,注:GCD中的时间为纳秒NSEC_PER_SEC,3.0 *NSEC_PER_SEC即为3秒
        dispatch_time_t start = dispatch_time(DISPATCH_TIME_NOW,3.0 *NSEC_PER_SEC);
        //设置定时器工作的间隔时间
        uint64_t intevel = 1.0 * NSEC_PER_SEC;
    
        /*
         第一个参数:要给哪个定时器设置
         第二个参数:定时器的开始时间DISPATCH_TIME_NOW表示从当前开始
         第三个参数:定时器调用方法的间隔时间
         第四个参数:定时器的精准度,如果传0则表示采用最精准的方式计算,如果传大于0的数值,则表示该定时切换可以接收该值范围内的误差,通常传0
         该参数的意义:可以适当的提高程序的性能
         注意点:GCD定时器中的时间以纳秒为单位(面试)
         */
        dispatch_source_set_timer(gcdTimer, start, intevel, 0 * NSEC_PER_SEC);
        //3.设置定时器开启后回调的方法
        /*
         第一个参数:要给哪个定时器设置
         第二个参数:回调block
         */
        dispatch_source_set_event_handler(gcdTimer, ^{
            NSLog(@"------%@",[NSThread currentThread]);
        });
    
        //4.执行定时器
        dispatch_resume(gcdTimer);
     //注意:dispatch_source_t本质上是OC类,在这里是个局部变量,需要强引用
        self.gcdTimer = gcdTimer;
    

    运行结果:

    2021-05-26 17:13:10.538833+0800 Interview03-定时器[32962:4500199] ------<NSThread: 0x600001e4c6c0>{number = 8, name = (null)}
    2021-05-26 17:13:11.538501+0800 Interview03-定时器[32962:4500201] ------<NSThread: 0x600001e6cb00>{number = 5, name = (null)}
    2021-05-26 17:13:12.537677+0800 Interview03-定时器[32962:4500201] ------<NSThread: 0x600001e6cb00>{number = 5, name = (null)}
    2021-05-26 17:13:12.783975+0800 Interview03-定时器[32962:4500102] -[ViewController dealloc]
    

    感兴趣的可以自行封装方便调用

    相关文章

      网友评论

          本文标题:内存管理-定时器(CADisplayLink、NSTimer)

          本文链接:https://www.haomeiwen.com/subject/tbvksltx.html