zoukankan      html  css  js  c++  java
  • OC原理解决定时器的循环引用

    对于以下代码:

    @property (strong, nonatomic) NSTimer *timer;
    
    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];
    }

    运行代码之后,发现当前对象并没有销毁,定时器并没有销毁,其实是由于当前对象对timer进行强引用,而timer中的target又对当前对象进行强应用,导致循环引用

    解决这种循环引用的有两种,一个timer创建的时候用block代码如下,并声明weakSelf属性,代码如下:

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

    第二种创建一个中间对象,里面声明一个weak的target的对象,再利用runtime的转发机制,有target去执行

    创建LBTemp的类,代码如下:

    #import <Foundation/Foundation.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @interface LBTemp : NSObject
    + (instancetype)tempWithTarget:(id)target;
    @property (weak, nonatomic) id target;
    @end
    
    #import "LBTemp.h"
    
    @implementation LBTemp
    + (instancetype)tempWithTarget:(id)target
    {
        LBTemp *temp = [[LBTemp alloc] init];
        temp.target = target;
        return temp;
    }
    
    - (id)forwardingTargetForSelector:(SEL)aSelector
    {
        return self.target;
    }
    @end

    创建timer代码如下:

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

    同样的CADisplayLink循环引用问题也可以用第二种方法解决

  • 相关阅读:
    jquery判断设备是否是手机
    jQuery -- touch事件之滑动判断(左右上下方向)
    sass制作雪碧图
    js时间字符串转为标准时间
    装箱和拆箱
    Dictionary泛型集合实现山寨版金山词霸
    泛型集合
    ArrayList集合与索引器及Hashtable
    黑马程序员--静态方法和静态类
    黑马程序员--多态练习(手机工厂)
  • 原文地址:https://www.cnblogs.com/muzichenyu/p/14449413.html
Copyright © 2011-2022 走看看