RAC 与 NSTimer

NSTimer时钟事件:

一般情况 我们会这样撸

[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];

- (void)timeMethod{

    NSLog(@"timeMethod来了");

}

//这个时候 timeMethod来了每秒钟会打印一次

好问题来了,这个 时候我拖一个UITextView放到控制器,当我们一直拖动UITextView的时候,“ timeMethod来了每秒钟会打印一次”是不是停下来了。

为什么?

--首先我们要搞清楚,NSTimer事件是交给谁 处理?--"Runloop"在处理,系统会把这个事件包装成“souce”事件源给RunLoop处理;

那RunLoop在处理这些事件的时候有什么区分和区别呢?

--是有的,从下面两个图可以看出Runloop处理事件会有优先级的,UI模式级别最高,所有当我们拖动UITextView的时候,RunLoop在处理UITextView的触摸事件(这个事件是UI模式下的事件 ),timeMethod来了没有打印了


RunLoop


UI模式和默认模式

好现在我们来做个试验,假设我们想要在触摸UI 的时候,Timer事件还是会被处理,那我们添加到RunLoopd的UI模式下

    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];
   
    //UITrackingRunLoopMode
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];

奇怪了,这个时候只有在触摸UI的时候才会打印,

其实:

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];

等同于下面的默认模式,触摸UI的时候RunLoop不会处理UI模式下的事件

NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];

    //UITrackingRunLoopMode

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

如果我们想普通模式和UI模式下,都有事件响应,那边我们需要把这个Timer添加到第3中模式,站位模式"NSRunLoopCommonModes"

//总共有5中模型 UI 默认 占位模式(默认&UI模式) 程序启动模式 内核模式

NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];


以为站位模式就万事大吉了没,NO,避免我们这个Timer做一个耗时操作,运行下面代码(在timeMethod让线程睡2秒)

NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];


- (void)timeMethod{

    [NSThread sleepForTimeInterval:2.0];

    NSLog(@"timeMethod来了");

}

这个时候是不是拓动UI的时候有卡顿现象。那么这种我们如何处理不影响UI卡顿呢?

--此时此刻,我们要记住每条线程都有一个RunLoop,默认情况是不开启的。

是不是现在又灵感有来了,我们把这个Timer丢到一个线程里面去

NSThread *thread =  [[NSThread alloc] initWithBlock:^{
            NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
           [[NSRunLoop currentRunLoop] run];//重点,RunLoop,默认情况是不开启的。注意要启动
           NSLog(@"come here!");//这里不会打印,Runloop是死循环
    }];
   
    [thread start];
 


- (void)timeMethod{
    [NSThread sleepForTimeInterval:2.0];
    NSLog(@"timeMethod来了%@",[NSThread currentThread]);
}

运行上面代码,这个时候是不是不会出现UI卡顿了。

问题有来l,RunLoop是不是停不下来了,要停下来,要怎么玩?请看下面


NSThread *thread =  [[NSThread alloc] initWithBlock:^{
            NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(timeMethod) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
       while (!_finished) {
           [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.001]];
       }
          // [[NSRunLoop currentRunLoop] run];
           NSLog(@"come here! %@",[NSThread currentThread]);
    }];
   
    [thread start];
 
}

- (void)timeMethod{
    NSLog(@"timeMethod来了%@",[NSThread currentThread]);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    _finished = YES;
}

一旦RunLoop停止,come here就好打印出来, 线程执行完就释放了。

其实我们还有种简单方式处理这种需求

/GCD timer 设置Time


//注意要强引用self.timer

  self.timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(0, 0));

    dispatch_source_set_timer(self.timer, DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);//2 开始时间 、持续时间、

    dispatch_source_set_event_handler(self.timer, ^{

        NSLog(@"GCD timer:%@", [NSThread currentThread]); //打印的是在子线程

    });

    dispatch_resume(self.timer); //启动

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。