美文网首页
iOS 在子线程中NSTimer的启动和关闭

iOS 在子线程中NSTimer的启动和关闭

作者: YYT1992 | 来源:发表于2018-01-30 10:55 被阅读37次

之前在项目中遇见了一个问题,在子线程中如何开启NSTimer和取消NSTimer。现在总结一下,当做自己的笔记。

1.子线程中NSTimer的开启

首先在.m文件中声明两个属性一个是子线程 一个是子线程中的NSTimer。

@property (nonatomic, strong) NSThread *thread1;

@property (nonatomic, strong) NSTimer *threadTimer;

然后用GCD在全局全队中创建一个子线程并创建NSTimer。

__weak __typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(0, 0), ^{
    __strong __typeof(weakSelf) strongSelf = weakSelf;
    if (strongSelf) {
        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];
    }
});

注意的是:在子线程中创建的NSTimer需要加入到对应线程的RunLoop中。RunLoop中常用的mode有:NSDefaultRunLoopMode、UITrackingRunLoopMode和NSRunLoopCommonModes三种模式。

NSDefaultRunLoop 默认模式

UITrackingRunLoop 界面追踪,用于scrollView拖拽 滑动

NSRunLoopCommonModes 不是一个特定的模式,只是一种标记,比较综合的一种模式(包含 前两种模式)

在NSTimer加入RunLoop后,需要将RunLoop运行起来。

2.子线程中NSTimer的关闭

之后创建一个cancelTimer的方法

  • (void)cancel{

        if (self.threadTimer) {
    
            [self.threadTimer invalidate];
    
            self.threadTimer = nil;
    
        }
    

}
如果这个方法跟创建NSTimer不在同一个线程执行是无法将Timer 执行invalidate操作的。

然后现在我们需要在thread1这个线程中执行这个操作,在这里写一个方法用于在子线程中调用此方法。

  • (void)cancelTimer{

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

}
最后我们在有需要关闭Timer的地方执行此方法即可。

在这里说明一下为什么NSTimer要在同一个线程中创建和关闭。因为创建的Timer的时候已经把Timer加入到该线程对应的RunLoop中,这个RunLoop设置了这个Timer为一个事件。因此要在同一个线程中才能cancel这个Timer。

转自:https://www.jianshu.com/p/046ae404bf43

相关文章

  • iOS 在子线程中NSTimer的启动和关闭

    之前在项目中遇见了一个问题,在子线程中如何开启NSTimer和取消NSTimer。现在总结一下,当做自己的笔记。 ...

  • iOS 在子线程中NSTimer的启动和关闭

    之前在项目中遇见了一个问题,在子线程中如何开启NSTimer和取消NSTimer。现在总结一下,当做自己的笔记。 ...

  • NSTimer 在子线程中添加到 使用 dateWithTime

    在写 NSTimer 和 NSRunLoop 的测试用例时,发现在子线程中创建的 NSTimer 添加到 使用 N...

  • 多线程与NSTimer

    1.Ios主线程,也称UI线程,在主线程中使用NSTimer,runloop是自动开启的,(如果NSTimer当前...

  • iOS多线程详解:实践篇

    iOS多线程实践中,常用的就是子线程执行耗时操作,然后回到主线程刷新UI。在iOS中每个进程启动后都会建立一个主线...

  • iOS多线程之GCD

    多线程 在iOS开发中为提高程序的运行效率会将比较耗时的操作放在子线程中执行,iOS系统进程默认启动一个主线程,用...

  • NSTimer

    深入NSTimer(iOS)iOS 中的 NSTimer关于NSRunLoop和NSTimer的深入理解

  • 无标题文章

    iOS NSTimer使用详解-开启、关闭、移除 定时器定时器详解ios定时器关闭定时器NSTimer 1、要使用...

  • IOS NSThread线程处理 子线程的启动与结束

    IOS NSThread线程处理 子线程的启动与结束 iOS多线程开发-多线程实现方式 -(void)blockO...

  • NSTimer的使用

    NSTimer的使用注意一:循环引用问题二:runloop的mode问题三:在子线程中使用时需要启动runloop...

网友评论

      本文标题:iOS 在子线程中NSTimer的启动和关闭

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