nstimer循环引用_NSTimer循环引用的几种解决方案

前言

在iOS中,NSTimer的使用是非常频繁的,但是NSTimer在使用中需要注意,避免循环引用的问题。之前经常这样写:

- (void)setupTimer {

self.timer= [NSTimer scheduledTimerWithTimeInterval:1target: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的分类中添加类方法:

@implementationNSTimer (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]; }

@interfaceZYWeakObject()

@property (weak, nonatomic)idweakObject;@end

@implementationZYWeakObject- (instancetype)initWithWeakObject:(id)obj {

_weakObject=obj;returnself;

}+ (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

作者:zhouyangyng

链接:https://juejin.im/post/5b641fc46fb9a04fd16033e7

来源:掘金

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值