美文网首页计算机技术一锅炖iOS Developer
iOS-细数RunLoop的种种“坑”

iOS-细数RunLoop的种种“坑”

作者: Aaron_ZhangKH | 来源:发表于2016-10-20 12:30 被阅读0次

RunLoop的存在意义

首先我们要知道一件事,如果现在有一个线程A,在接受到消息(方法)的时候A被唤醒,并执行该任务。 那么执行完成之后呢?执行完成之后我们对线程A最理想的期待是它能处于休眠!也就是说,我有任务给线程A处理时,它能立马被唤醒来处理,没有任务时,它可以保持在休眠状态而不会销毁。而RunLoop就是为此而存在的!

RunLoop是什么

知道了RunLoop的存在意义后,我们要理解的就是RunLoop内部是如何帮线程实现上述功能的。答案是循环。RunLoop的中文含义本身就是循环运行,也就是说RunLoop通过使用一个"接受消息->等待->处理"的循环, 让线程既能不被销毁又能处于休眠状态。我们可以来看看这个循环用代码是怎么实现的

do{

var message = get_next_message( );
process_message(message);

}while(message != quit);

从这段代码可以看出,只要message不设定为quit,那么这个循环就会一直执行,而这个循环也就是RunLoop的本质。

RunLoop居然还分Mode?

是的,你没有看错,这小婊砸确实有几种模式要区分

  • Mode分类
    • NSDefaultRunLoopMode
      • 默认模式,主线程中的RunLoop默认是NSDefaultRunLoopMode
    • NSRunLoopCommonModes
      • 一组常用模式的集合,包含所有常见的RunLoopMode
    • UITrackingRunLoopMode
      • 滚动视图滚动时,当前线程的RunLoop会处于该模式下

RunLoop抛给我们的一些常见的“巨坑”

1. NSTimer和RunLoop的关系?

1.1 第一种关系 : NSTimer需要手动添加到RunLoop中, 才能执行的情况
  NSTimer *timer = [NSTimer timerWithTimeInterval:1.f 
                             target:self 
                             selector:@selector(update)
                             userInfo:nil 
                             repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];

我们来看看苹果官方API里对这个timerWithTimeInterval...方法的解释

  • Creates and returns a new NSTimer object initialized with the specified object and selector.
  • You must add the new timer to a run loop, using addTimer:forMode:
  • Then, after ti seconds have elapsed, the timer fires, sending the message aSelector to target.
    从官方的解释看看出, 要启动这个方法, WE must add the new timer to a run loop, 也就是说我们必须把创建的timer手动添加到runloop中
1.2 第二种关系 : NSTimer默认被添加到RunLoop中, 直接执行的情况
[NSTimer scheduledTimerWithTimeInterval:1.f 
          target:self 
          selector:@selector(update) 
          userInfo:nil 
          repeats:YES];

我们继续看官方API中对scheduledTimerWithTimeInterval...这个方法的解释:

  • Creates and returns a new NSTimer object and schedules it on the current run loop in the default mode.
    也就是说, 这个方法在创建timer的时候, 默认就已经将timer以defaultMode的模式添加到当前的线程的RunLoop中了

2. TableView/ScrollView/CollectionView滚动时NSTimer停止了?

2.1首先我们要明确以下两个知识点:
  • 一个RunLoop不能同时处于两个mode下, 也就是说最多只能处于一个模式下
  • 当滚动视图滚动时,当前的RunLoop处于UITrackingRunLoopMode
2.2 结合这两个知识点来看, 也就不难得出NSTimer停止的原因了
  • NSTimer的RunLoopMode和在滚动视图滚动时当前线程的RunLoopMode不一致,所以被无情地停止了!
2.3 解决方法:
  • 将timer的runloopMode改为UITrackingRunLoopMode或者NSRunLoopCommonModes, 使其与当前线程的RunLoopMode保持一致
实现代码
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];

3. 如果NSTimer在分线程中创建,NSTimer也停止了?

3.1 我们先来看一段代码

[NSThread detachNewThreadSelector: 
@selector(分线程创建runLoop) toTarget:self withObject:nil];

- (void)分线程创建runLoop{
    [NSTimer scheduledTimerWithTimeInterval:1.f 
              target:self
              selector:@selector(update) 
              userInfo:nil 
              repeats:YES];
}
- (void)update{
    NSLog(@"%ld",++_index);
}

运行之后, 我们会发现NSTimer没有启动! 什么鬼?!

3.2 原因

  • 在主线程中,系统默认创建并启动主线程的RunLoop
  • 但是在分线程中,系统不会自动启动RunLoop,需要手动启动!!!

3.3 解决方法

  • 启动分线程的runLoop

3.4 启动分线程中的RunLoop代码

[[NSRunLoop currentRunLoop] run];

写在结尾的话

RunLoop的定义, 存在意义以及使用中常见的坑我就大概想到这么多, 广大攻城狮们如果有想添加的内容欢迎手动评论我, 我会添加上去的. 最后, 有什么写得不对的, 也同样欢迎大家随时留下评论.

相关文章

  • iOS-细数RunLoop的种种“坑”

    RunLoop的存在意义 首先我们要知道一件事,如果现在有一个线程A,在接受到消息(方法)的时候A被唤醒,并执行该...

  • iOS底层原理——浅谈RunLoop

    RunLoop应用:线程保活 线程保活、控制销毁 iOS-浅谈RunLoop8iOS底层原理总结 - RunLoo...

  • ios- runloop

  • RunLoop 的坑

    由于项目中, 使用 runLoop.run()方法, 当执行 remove port 和CFRunLoopStop...

  • iOS-浅谈RunLoop

    前言:本文简述RunLoop相关内容,如有错误请留言指正。 Q:什么是RunLoop? 运行循环,在程序运行过程中...

  • iOS- Runloop浅谈

    最近项目完成年前的时间也比较轻松,对一些知识就行总结。今天总结的是Runloop.对于Runloop,在平时项目中...

  • iOS-多线程
RunLoop

    什么是RunLoop 从字面意思看 运行循环 跑圈 基本作用 保持程序的持续运行 处理App中的各种事件(比如触摸...

  • RunLoop 填坑

    之前关于RunLoop只知道一点,最近花时间重新系统的学习了一下,以下是我的学习笔记及总结。有不足的部分,望大佬不...

  • cocoaPods 打包静态库 遇到的坑 and git

    cocoaPods 打包静态库 遇到的坑参考资料iOS 打包.a文件的那些事儿iOS-将自己的库支持CocoaPo...

  • RDP Redirection 的种种坑

    Audio Redirection的问题 怎么写都不好用 原因是不但要Join rdpsnd Channel而且要...

网友评论

    本文标题:iOS-细数RunLoop的种种“坑”

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