关于NSTimer和CADisplayLink定时器循环引用;
我这里有控制器ViewControll1和ViewControll2两个控制器, ViewControll1 push出来ViewControll2,然后在ViewControll2写下这些代码,对button,NSTimer,和CADisplayLink进行验证,其中TestBtn继承自UIButton,重写dealloc方法,观察能否销毁
@interface ViewController2 ()
@property(nonatomic,strong)TestBtn *btn;
@property(nonatomic,strong)NSTimer *timer;
@property(nonatomic,strong)CADisplayLink *displayLink;
@end
@implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
self.btn = [[TestBtn alloc]initWithFrame:CGRectMake(0, 0, 100, 50)];
self.btn.center = self.view.center;
[self.btn setBackgroundColor:[UIColor blueColor]];
[self.btn setTitle:@"按钮" forState:UIControlStateNormal];
[self.btn addTarget:self action:@selector(action) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:self.btn];
__weak typeof(self) weakSelf = self;
// self.timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
// [weakSelf timerAction];
// }];
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:weakSelf selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
// self.displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(displayLinkAction)];
// [self.displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
}
- (void)action
{
NSLog(@"执行点击");
}
- (void)timerAction
{
NSLog(@"执行timer定时器");
}
- (void)displayLinkAction
{
NSLog(@"执行displayLink定时器");
}
- (void)dealloc
{
[self.timer invalidate];
NSLog(@"%s",__func__);
}
1.只打开TestBtn的方法,运行进入VC2,再POP回VC1,打印内容
image.png
代表button和VC2没有造成循环引用
2.只打开block类型的timer代码,并重新进行上面的操作
self.timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
[self timerAction];
}];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
image.png
回到VC1时,timer还在执行,并且VC2也没销毁,造成了循环引用
VC2->timer->timer的block对[self timerAction]变量捕捉VC2,造成了循环引用,可以通过__weak对[self timerAction]的捕捉VC2为弱引用
__weak typeof(self) weakSelf = self;
self.timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
[weakSelf timerAction];
}];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
打印信息,显示这样就不会造成循环 引用
image.png
但是有其他情况,比如不是通过block捕捉的self,而是通过target引用的,那么__weak就用不了了,比如这样,我试过了VC2不会销毁
__weak typeof(self) weakSelf = self;
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:weakSelf selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
这种可以通过新建一个类,然后VC2->强timer->新建类强引用->弱VC2的方式进行解决,我这里新建了一个类TestProxy
@interface TestProxy : NSObject
@property(nonatomic,weak)id target;
+ (instancetype)proxyWithTarget:(id)target;
@end
@implementation TestProxy
+ (instancetype)proxyWithTarget:(id)target
{
TestProxy *proxy = [[TestProxy alloc] init];
proxy.target = target;
return proxy;
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
return _target;
}
然后修改self.timer的创建方式
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:[TestProxy proxyWithTarget:self] selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
打印结果为,证明也是可以销毁的
image.png
但是上面的内容可以再优化,由于TestProxy继承自NSObject,它需要经过消息发送的遍历查找,动态解析,最后才会到我们这里的消息转发过程,大大消耗性能;苹果提供了一个专门做消息转发的类NSProxy,这个类和NSObject同一级别,该类的特性就是去掉消息遍历查找,消息添加,直接消息转发的过程,大大提高了效率
@interface GYJProxy : NSProxy
+ (instancetype)proxyWithTarget:(id)target;
@end
@interface GYJProxy ()
@property(nonatomic,weak)id target;
@end
@implementation GYJProxy
+ (instancetype)proxyWithTarget:(id)target
{
GYJProxy *proxy = [GYJProxy alloc];
proxy->_target = target;
return proxy;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel
{
return [_target methodSignatureForSelector:sel];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
[invocation invokeWithTarget:_target];
}
@end
最后验证的结果也是可以正常销毁的,CADisplayLink和NSTimer一样,也会造成循环引用,处理方式和NSTimer一样.还有就是NSTimer和CADisplayLink都是基于runloop使用的,这会造成时间稍微不准,建议采用GCD的定时器,GCD的定时器不依赖runloop,时间是准的
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
//1.源 2.开始时间 3.间隔时间 4.精准度
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, NSEC_PER_SEC*1.0, 0*NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
NSLog(@"定时器执行的任务");
});
dispatch_resume(timer);
网友评论