美文网首页
NSTimer的循环引用

NSTimer的循环引用

作者: Rumbles | 来源:发表于2018-09-05 10:31 被阅读2次

NSTimer基本使用

[NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeredAction:) userInfo:nil repeats:YES];
- (void)timeredAction:(NSTimer*)timer {
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
    view.backgroundColor = CNCbox_RandomColor;
    [self.view addSubview:view];
}

这时系统已经在运行定时器的方法了

NSTimer与RunLoop

我们创建好的NSTimer默认是运行在 NSDefaultRunLoopMode 下的 在ui滚动的时候系统是运行在 UITrackingRunLoopMode 下的。这时我们的Timer是不会执行方法的 只有在UI停止滚动的时候才会执行

想要在UI滚动的时候执行 需要加入到NSRunLoopCommonModes中
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];

通常情况下NSDefaultRunLoopMode和UITrackingRunLoopMode都已经被加入到了common modes集合中, 所以不论runloop运行在哪种mode下, NSTimer都会被及时触发

NSTimer 循环引用的问题

当我们的timer  repeats为NO的时候我们的timer运行完就回在即释放掉了

NSTimer的scheduledTimerWithTimeInterval: target: selector: userInfo: repeats:方法的最后一个参数为YES时,NSTimer会一直保留目标对象,直到自身失效才释放目标对象。执行完任务后,一次性的定时器会自动失效;重复性的定时器,需要主动调用invalidate方法才会失效。


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.timer invalidate];
    self.timer = nil;
}

iOS10中,定时器的API新增了block方法,
    __weak typeof(self) weakself = self;
    _timer = [NSTimer timerWithTimeInterval:0.01 repeats:YES block:^(NSTimer * _Nonnull timer) {
        [weakself timeredAction:nil];
    }];

如何在子线程使用NSTimer

- (void)threadStart {
    __weak __typeof(self) weakSelf = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        __strong __typeof(weakSelf) strongSelf = weakSelf;
        if (!strongSelf) return;
        strongSelf.thread1 = [NSThread currentThread];
        [strongSelf.thread1 setName:@"线程A"];
        strongSelf.threadTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:strongSelf selector:@selector(timerAction) userInfo:nil repeats:YES];
        NSRunLoop *runloop = [NSRunLoop currentRunLoop];
        [runloop addTimer:strongSelf.threadTimer forMode:NSDefaultRunLoopMode];
        [runloop run];
    });
}

- (void)timerAction {
    
}

如果这个方法跟创建NSTimer不在同一个线程执行是无法将Timer 执行invalidate操作的
然后现在我们需要在thread1这个线程中执行这个操作,在这里写一个方法用于在子线程中调用此方法


- (void)cancelTimer{    
    if (self.threadTimer && self.thread1) {
        [self performSelector:@selector(cancel) onThread:self.thread1 withObject:nil waitUntilDone:YES];
    }
}

- (void)cancel {
    if (!self.threadTimer) return;
    [self.threadTimer invalidate];
    self.threadTimer = nil;
}

上面的一大堆会释放 self.threadTimer.但是因为手动开启子线程的运行循环 (这个是主线程的运行循环和子线程的运行循环唯一的不同点)
 run : 一旦调用这个方法开启子线程的运行循环,就不会停止
 一旦开启运行循环,相当于就开启了死循环



`可以释放的代码
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    NSLog(@" touchesBegan ");
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(update) userInfo:nil repeats:YES];
        [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
        CFRunLoopRun();
    });
}



- (void)update {
    static int i = 0;
    NSLog(@"%d", i);
    i++;
    if (i > 4) {
        CFRunLoopStop(CFRunLoopGetCurrent());
        i = 0;
    }
    NSLog(@"update: %@",[NSThread currentThread]);
}

推荐使用GCD

    [self startGCDTimerCompletion:^{
        NSLog(@"startGCDTimerCompletion: %@",[NSThread currentThread]);
    }];
}


- (void)startGCDTimerCompletion:(void (^)(void))completion {
    if (_gcdTimer) return;
    NSTimeInterval period = 0.1; //设置时间间隔
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    _gcdTimer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(_gcdTimer, dispatch_walltime(NULL, 0), period * NSEC_PER_SEC, 0);
    // 事件回调
    dispatch_source_set_event_handler(_gcdTimer, ^{
        NSLog(@"event_handler: %@",[NSThread currentThread]);
        dispatch_async(dispatch_get_main_queue(), ^{
            if (completion) completion();
        });
    });
    // 开启定时器
    dispatch_resume(_gcdTimer);
}

- (void)endGCDtimer {
    if (!_gcdTimer) return;
    dispatch_source_cancel(_gcdTimer);
    _gcdTimer = nil;
}

iOS | 小心NSTimer中的内存泄漏

相关文章

网友评论

      本文标题:NSTimer的循环引用

      本文链接:https://www.haomeiwen.com/subject/ezknwftx.html