NSTimer循环引用的几种解决方案
https://www.cnblogs.com/jukaiit/p/10599021.html
在iOS中,NSTimer的使用是非常频繁的,但是NSTimer在使用中需要注意,避免循环引用的问题。之前经常这样写:
- (void)setupTimer { self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; } - (void)dealloc { [self.timer invalidate]; self.timer = nil; }
由于self强引用了timer,同时timer也强引用了self,所以循环引用造成dealloc方法根本不会走,self和timer都不会被释放,造成内存泄漏。
下面介绍一下几种解决timer循环引用的方法。
1. 选择合适的时机手动释放timer(该方法并不太合理)
在之前自己就是这样解决循环引用的:
- 控制器中
- (void)viewDidDisappear:(BOOL)animated { [super viewDidDisappear:animated]; [self.timer invalidate]; self.timer = nil; }
- view中
- (void)removeFromSuperview { [super removeFromSuperview]; [self.timer invalidate]; self.timer = nil; }
在某些情况下,这种做法是可以解决问题的,但是有时却会引起其他问题,比如控制器push到下一个控制器,viewDidDisappear后,timer被释放,此时再回来,timer已经不复存在了。
所以,这种"方案"并不是合理的。
2. timer使用block方式添加Target-Action
这里我们需要自己在NSTimer的分类中添加类方法:
@implementation NSTimer (BlcokTimer) + (NSTimer *)bl_scheduledTimerWithTimeInterval:(NSTimeInterval)interval block:(void (^)(void))block repeats:(BOOL)repeats { return [self scheduledTimerWithTimeInterval:interval target:self selector:@selector(bl_blockSelector:) userInfo:[block copy] repeats:repeats]; } + (void)bl_blockSelector:(NSTimer *)timer { void(^block)(void) = timer.userInfo; if (block) { block(); } } @end
通过block的方式,获取action,实际的target设置为self,即NSTimer类。这样我们在使用timer时,由于target的改变,就不再有循环引用了。 使用中还需要注意block可能引起的循环引用,所以使用weakSelf:
__weak typeof(self) weakSelf = self; self.timer = [NSTimer bl_scheduledTimerWithTimeInterval:1 block:^{ [weakSelf changeText]; } repeats:YES];
虽然没有了循环引用,但是还是应该记得在dealloc时释放timer。
3. 给self添加中间件proxy
考虑到循环引用的原因,改方案就是需要打破这些相互引用关系,因此添加一个中间件,弱引用self,同时timer引用了中间件,这样通过弱引用来解决了相互引用,如图:
接下来看看怎么实现这个中间件,直接上代码:
@interface ZYWeakObject()
@property (weak, nonatomic) id weakObject;
@end
@implementation ZYWeakObject
- (instancetype)initWithWeakObject:(id)obj {
_weakObject = obj;
return self;
}
+ (instancetype)proxyWithWeakObject:(id)obj {
return [[ZYWeakObject alloc] initWithWeakObject:obj];
}
@interface ZYWeakObject() @property (weak, nonatomic) id weakObject; @end @implementation ZYWeakObject - (instancetype)initWithWeakObject:(id)obj { _weakObject = obj; return self; } + (instancetype)proxyWithWeakObject:(id)obj { return [[ZYWeakObject alloc] initWithWeakObject:obj]; }
仅仅添加了weak类型的属性还不够,为了保证中间件能够响应外部self的事件,需要通过消息转发机制,让实际的响应target还是外部self,这一步至关重要,主要涉及到runtime的消息机制。
/** * 消息转发,让_weakObject响应事件 */ - (id)forwardingTargetForSelector:(SEL)aSelector { return _weakObject; } - (void)forwardInvocation:(NSInvocation *)invocation { void *null = NULL; [invocation setReturnValue:&null]; } - (BOOL)respondsToSelector:(SEL)aSelector { return [_weakObject respondsToSelector:aSelector]; }
接下来就可以这样使用中间件了:
// target要设置成weakObj,实际响应事件的是self ZYWeakObject *weakObj = [ZYWeakObject proxyWithWeakObject:self]; self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:weakObj selector:@selector(changeText) userInfo:nil repeats:YES];
结论
经测试,以上两种方案都是可以解决timer的循环引用问题
代码请移步github: Demo https://github.com/zhouyangyng/timerRetainCycle
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具