關于NSTimer和CADisplayLink定時器循環(huán)引用;
我這里有控制器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(@"執(zhí)行點擊");
}
- (void)timerAction
{
NSLog(@"執(zhí)行timer定時器");
}
- (void)displayLinkAction
{
NSLog(@"執(zhí)行displayLink定時器");
}
- (void)dealloc
{
[self.timer invalidate];
NSLog(@"%s",__func__);
}
1.只打開TestBtn的方法,運行進入VC2,再POP回VC1,打印內(nèi)容
代表button和VC2沒有造成循環(huán)引用
2.只打開block類型的timer代碼,并重新進行上面的操作
self.timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
[self timerAction];
}];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
回到VC1時,timer還在執(zhí)行,并且VC2也沒銷毀,造成了循環(huán)引用
VC2->timer->timer的block對[self timerAction]變量捕捉VC2,造成了循環(huán)引用,可以通過__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];
打印信息,顯示這樣就不會造成循環(huán) 引用
但是有其他情況,比如不是通過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的創(chuàng)建方式
self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:[TestProxy proxyWithTarget:self] selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
打印結(jié)果為,證明也是可以銷毀的
但是上面的內(nèi)容可以再優(yōu)化,由于TestProxy繼承自NSObject,它需要經(jīng)過消息發(fā)送的遍歷查找,動態(tài)解析,最后才會到我們這里的消息轉(zhuǎn)發(fā)過程,大大消耗性能;蘋果提供了一個專門做消息轉(zhuǎn)發(fā)的類NSProxy,這個類和NSObject同一級別,該類的特性就是去掉消息遍歷查找,消息添加,直接消息轉(zhuǎn)發(fā)的過程,大大提高了效率
@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
最后驗證的結(jié)果也是可以正常銷毀的,CADisplayLink和NSTimer一樣,也會造成循環(huán)引用,處理方式和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(@"定時器執(zhí)行的任務");
});
dispatch_resume(timer);