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循环引用问题也可以用第二种方法解决

  • 相关阅读:
    CSS常用原子类base.css
    前端开发规范集锦
    C#(.NET) HMAC SHA256实现
    windows200364位iis6 php环境搭建
    MSSQL数据库分页存储过程
    C#动态调用webservice
    SQL语句之备份表
    双网卡内外网同时使用的方法
    SQL2008 提示评估期已过的解决方法
    时间服务器设置局域网时间同步
  • 原文地址:https://www.cnblogs.com/muzichenyu/p/14449413.html
Copyright © 2011-2022 走看看