解決定時(shí)器循環(huán)引用的問題

關(guān)于NSTimer和CADisplayLink定時(shí)器循環(huán)引用;
我這里有控制器ViewControll1和ViewControll2兩個(gè)控制器, ViewControll1 push出來ViewControll2,然后在ViewControll2寫下這些代碼,對button,NSTimer,和CADisplayLink進(jìn)行驗(yàn)證,其中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í)行點(diǎn)擊");
}
- (void)timerAction
{
    NSLog(@"執(zhí)行timer定時(shí)器");
}
- (void)displayLinkAction
{
    NSLog(@"執(zhí)行displayLink定時(shí)器");
}

- (void)dealloc
{
  [self.timer invalidate];
    NSLog(@"%s",__func__);
}

1.只打開TestBtn的方法,運(yùn)行進(jìn)入VC2,再POP回VC1,打印內(nèi)容


image.png

代表button和VC2沒有造成循環(huán)引用

2.只打開block類型的timer代碼,并重新進(jìn)行上面的操作

    self.timer = [NSTimer timerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [self timerAction];
    }];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
image.png

回到VC1時(shí),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ì)造成循環(huán) 引用


image.png

但是有其他情況,比如不是通過block捕捉的self,而是通過target引用的,那么__weak就用不了了,比如這樣,我試過了VC2不會(huì)銷毀

    __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];

這種可以通過新建一個(gè)類,然后VC2->強(qiáng)timer->新建類強(qiáng)引用->弱VC2的方式進(jìn)行解決,我這里新建了一個(gè)類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é)果為,證明也是可以銷毀的


image.png

但是上面的內(nèi)容可以再優(yōu)化,由于TestProxy繼承自NSObject,它需要經(jīng)過消息發(fā)送的遍歷查找,動(dòng)態(tài)解析,最后才會(huì)到我們這里的消息轉(zhuǎn)發(fā)過程,大大消耗性能;蘋果提供了一個(gè)專門做消息轉(zhuǎn)發(fā)的類NSProxy,這個(gè)類和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

最后驗(yàn)證的結(jié)果也是可以正常銷毀的,CADisplayLink和NSTimer一樣,也會(huì)造成循環(huán)引用,處理方式和NSTimer一樣.還有就是NSTimer和CADisplayLink都是基于runloop使用的,這會(huì)造成時(shí)間稍微不準(zhǔn),建議采用GCD的定時(shí)器,GCD的定時(shí)器不依賴runloop,時(shí)間是準(zhǔn)的

    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    //1.源 2.開始時(shí)間 3.間隔時(shí)間 4.精準(zhǔn)度
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, NSEC_PER_SEC*1.0, 0*NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        NSLog(@"定時(shí)器執(zhí)行的任務(wù)");
    });
    dispatch_resume(timer);
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容