强引用(强持有)

简介: 强引用(强持有)

强引用(强持有)


解决NSTimer强引用:


  • 重写didMoveToParentViewController方法


- (void)didMoveToParentViewController:(UIViewController *)parent{  
    // 无论push 进来 还是 pop 出去 正常跑  
    // 就算继续push 到下一层 pop 回去还是继续  
    if (parent == nil) {  
       [self.timer invalidate];  
        self.timer = nil;  
        NSLog(@"timer 走了");  
    }  
}


  • 定义timer时,采用闭包的形式,因此不需要指定target


- (void)blockTimer{  
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {  
        NSLog(@"timer fire - %@",timer);  
    }];  
}


解决 强引用(强持有)


依赖中介者模式,打破强持有,其中推荐思路四


1. pop时在其他方法中销毁timer


- (void)didMoveToParentViewController:(UIViewController *)parent{  
    // 无论push 进来 还是 pop 出去 正常跑  
    // 就算继续push 到下一层 pop 回去还是继续  
    if (parent == nil) {  
       [self.timer invalidate];  
        self.timer = nil;  
        NSLog(@"timer 走了");  
    }  
}


2.中介者模式,即不使用self,依赖于其他对象


//**********1、定义其他对象**********  
@property (nonatomic, strong) id            target;  
//**********1、修改target**********  
self.target = [[NSObject alloc] init];  
class_addMethod([NSObject class], @selector(fireHome), (IMP)fireHomeObjc, "v@:");  
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self.target selector:@selector(fireHome) userInfo:nil repeats:YES];  
//**********3、imp**********  
void fireHomeObjc(id obj){  
    NSLog(@"%s -- %@",__func__,obj);  
}  
- (void)dealloc{  
    [self.timer invalidate];  
    self.timer = nil;  
    NSLog(@"%s",__func__);  
}


3.自定义封装timer


//*********** .h文件 ***********  
@interface CJLTimerWapper : NSObject  
- (instancetype)cjl_initWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo;  
- (void)cjl_invalidate;  
@end  
//*********** .m文件 ***********  
#import "CJLTimerWapper.h"  
#import <objc/message.h>  
@interface CJLTimerWapper ()  
@property(nonatomic, weak) id target;  
@property(nonatomic, assign) SEL aSelector;  
@property(nonatomic, strong) NSTimer *timer;  
@end  
@implementation CJLTimerWapper  
- (instancetype)cjl_initWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(nullable id)userInfo repeats:(BOOL)yesOrNo{  
    if (self == [super init]) {  
        //传入vc  
        self.target = aTarget;  
        //传入的定时器方法  
        self.aSelector = aSelector;  
        if ([self.target respondsToSelector:self.aSelector]) {  
            Method method = class_getInstanceMethod([self.target class], aSelector);  
            const char *type = method_getTypeEncoding(method);  
            //给timerWapper添加方法  
            class_addMethod([self class], aSelector, (IMP)fireHomeWapper, type);  
            //启动一个timer,target是self,即监听自己  
            self.timer = [NSTimer scheduledTimerWithTimeInterval:ti target:self selector:aSelector userInfo:userInfo repeats:yesOrNo];  
        }  
    }  
    return self;  
}  
//一直跑runloop  
void fireHomeWapper(CJLTimerWapper *wapper){  
    //判断target是否存在  
    if (wapper.target) {  
        //如果存在则需要让vc知道,即向传入的target发送selector消息,并将此时的timer参数也一并传入,所以vc就可以得知`fireHome`方法,就这事这种方式定时器方法能够执行的原因
        //objc_msgSend发送消息,执行定时器方法  
        void (*lg_msgSend)(void *,SEL, id) = (void *)objc_msgSend;  
         lg_msgSend((__bridge void *)(wapper.target), wapper.aSelector,wapper.timer);  
    }else{  
        //如果target不存在,已经释放了,则释放当前的timerWrapper  
        [wapper.timer invalidate];  
        wapper.timer = nil;  
    }  
}  
//在vc的dealloc方法中调用,通过vc释放,从而让timer释放  
- (void)cjl_invalidate{  
    [self.timer invalidate];  
    self.timer = nil;  
}  
- (void)dealloc  
{
    NSLog(@"%s",__func__);  
}  
@end


4. 利用NSProxy虚基类的子类


//************NSProxy子类************  
@interface CJLProxy : NSProxy  
+ (instancetype)proxyWithTransformObject:(id)object;  
@end  
@interface CJLProxy()  
@property (nonatomic, weak) id object;  
@end  
@implementation CJLProxy  
+ (instancetype)proxyWithTransformObject:(id)object{  
    CJLProxy *proxy = [CJLProxy alloc];  
    proxy.object = object;  
    return proxy;  
}  
-(id)forwardingTargetForSelector:(SEL)aSelector {  
    return self.object;     
}
//************解决timer强持有问题************  
self.proxy = [CJLProxy proxyWithTransformObject:self];  
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self.proxy selector:@selector(fireHome) userInfo:nil repeats:YES];  
//在dealloc中将timer正常释放  
- (void)dealloc{  
    [self.timer invalidate];  
    self.timer = nil;  
}




目录
相关文章
|
XML Java BI
jasper在JSP中导出pdf和xls
环境配置 1  正确安装配置java的jdk 2 将ireport放到c:/ireport 3 将ant放到c:/ant(我用的是bea的ant路径是D:/bea/weblogic81/server/ant) 4 将jasperreports-1.
2177 0
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8071 2
|
11天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18588 64
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10018 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1509 5
|
8天前
|
云安全 数据采集 人工智能
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2654 8