RunLoop

原文链接

什么是RunLoop?

A RunLoop object processes input for sources such as mouse and keyboard events from the window system, Port objects, and NSConnection objects. A RunLoop object also processes Timer events.
Your application neither creates or explicitly manages RunLoop objects. Each Thread object—including the application’s main thread—has an RunLoop object automatically created for it as needed. If you need to access the current thread’s run loop, you do so with the class method current.
Note that from the perspective of RunLoop, Timer objects are not "input"—they are a special type, and one of the things that means is that they do not cause the run loop to return when they fire.

官方文档

翻译如下

RunLoop对象处理来自窗口系统,Port对象和NSConnection对象的源(如鼠标和键盘事件)的输入。 RunLoop对象还处理Timer事件。 您的应用程序既不创建也不显式管理RunLoop对象。每个Thread对象(包括应用程序的主线程)都会根据需要自动为其创建RunLoop对象。如果需要访问当前线程的运行循环,可以使用类方法current。 请注意,从RunLoop的角度来看,Timer对象不是“输入” - 它们是一种特殊类型,其中一个意思是它们不会导致运行循环在它们触发时返回。

我们都知道我们的应用是一个进程,在应用程序周期中我们大多都会开辟不同的线程来处理一些耗时的事情,在这里面Runloop扮演的是什么角色呢?

Runloop 可以翻译为 事件循环 我们可以把其理解为一个 while 循环

do {
    // something
}while()


Runloop是和线程一一对应的,其中,主线程的Runloop会在应用启动的时候进行启动和绑定,其他线程的Runloop要通过手动调用 [NSRunLoop currentRunLoop] 才会生成

看一下Foundation中的NSRunLoop.h, 里面有这几个方法需要注意一下:


@property (class, readonly, strong) NSRunLoop *currentRunLoop;
@property (class, readonly, strong) NSRunLoop *mainRunLoop;
@property (nullable, readonly, copy) NSRunLoopMode currentMode;

- (void)addTimer:(NSTimer *)timer forMode:(NSRunLoopMode)mode;

- (void)addPort:(NSPort *)aPort forMode:(NSRunLoopMode)mode;


其中 currentRunLoop 表示获取当前的Runloop ,mainRunLoop代表获取主事件循环,这里可以看出,非主线程的Runloop必须在子线程内获取,而mainRunLoop可以在任意线程获取。

NSRunLoopMode 在官方文档中提到的有一下子五个:

  • NSDefaultRunLoopMode
  • NSConnectionReplyMode
  • NSModalPanelRunLoopMode
  • NSEventTrackingRunLoopMode
  • NSRunLoopCommonModes

其中公开暴露出来的只有 NSDefaultRunLoopMode 和 NSRunLoopCommonModes ,在这里面 NSRunLoopCommonModes 实际上包含了 NSDefaultRunLoopMode 和 NSEventTrackingRunLoopMode,在需要追踪包括scrollview滚动等事件的时候最好使用 NSRunLoopCommonModes

Runloop 的实际应用


首先,我们可以在viewcontroller中的touchbegin方法打断点,看一下该方法的调用栈

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


在断点出发时我们在控制台查看

(lldb) thread backtrace
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
  * frame #0: 0x000000010a1589a0 Library_test`-[ViewController touchesBegan:withEvent:](self=0x00007f7fda41adc0, _cmd="touchesBegan:withEvent:", touches=1 element, event=0x0000600002649440) at ViewController.m:35
    frame #1: 0x0000000110e088e8 UIKitCore`forwardTouchMethod + 353
    frame #2: 0x0000000110e08776 UIKitCore`-[UIResponder touchesBegan:withEvent:] + 49
    frame #3: 0x0000000110e17dff UIKitCore`-[UIWindow _sendTouchesForEvent:] + 2052
    frame #4: 0x0000000110e197a0 UIKitCore`-[UIWindow sendEvent:] + 4080
    frame #5: 0x0000000110df7394 UIKitCore`-[UIApplication sendEvent:] + 352
    frame #6: 0x0000000110ecc5a9 UIKitCore`__dispatchPreprocessedEventFromEventQueue + 3054
    frame #7: 0x0000000110ecf1cb UIKitCore`__handleEventQueueInternal + 5948
    frame #8: 0x000000010cb87721 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    frame #9: 0x000000010cb86f93 CoreFoundation`__CFRunLoopDoSources0 + 243
    frame #10: 0x000000010cb8163f CoreFoundation`__CFRunLoopRun + 1263
    frame #11: 0x000000010cb80e11 CoreFoundation`CFRunLoopRunSpecific + 625
    frame #12: 0x00000001143891dd GraphicsServices`GSEventRunModal + 62
    frame #13: 0x0000000110ddb81d UIKitCore`UIApplicationMain + 140
    frame #14: 0x000000010a1590d0 Library_test`main(argc=1, argv=0x00007ffee5aa7000) at main.m:14
    frame #15: 0x000000010e331575 libdyld.dylib`start + 1
(lldb) 


可以看到 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION 的方法调用

其实这就是一个点击的事件触发了,通过runloop传递的例子,实际上所有的事件都会通过这个方法调用,最后触发到我们编写的代码, 理解了这个我们接下来看看具体的应用。

NSTimer


在controller中添加一个timer

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSTimer *t;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _t = [NSTimer timerWithTimeInterval:1.0f repeats:true block:^(NSTimer * _Nonnull timer) {

        NSLog(@"Timer Triggered");
    }];
    [[NSRunLoop currentRunLoop] addTimer:_t forMode:NSRunLoopCommonModes];
}

@end

处理耗时逻辑


在后台进程处理逻辑,完成后通过modes避免在滑动视图的时候返回

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self performSelectorInBackground:@selector(someBussiness) withObject:nil];
}

- (void)someBussiness {
    
    [self performSelector:@selector(bussinessFinished) withObject:nil afterDelay:0.0f inModes:@[NSDefaultRunLoopMode]];
}

- (void)bussinessFinished {
    
}


自定义后台线程


有时我们想把一些逻辑全部放到自定义的线程中去处理,下面给出一个解决方案。

#import "ViewController.h"

@interface ViewController ()

@property (nonatomic, strong) NSThread *thread;

@property (nonatomic, strong) NSPort *port;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.thread start];
}

- (void)selfThread {
    
    @autoreleasepool {
    
        [[NSRunLoop currentRunLoop] run];
    }
}

- (NSThread *)thread {
    if (!_thread) {
        _thread = [[NSThread alloc] initWithTarget:self selector:@selector(selfThread) object:nil];
    }
    return _thread;
}

- (NSPort *)port {
    if (!_port) {
        _port = [NSPort port];
    }
    return _port;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self performSelector:@selector(someBussiness) onThread:self.thread withObject:nil waitUntilDone:false];
}

- (void)someBussiness {
    
}

@end


运行之后貌似点击屏幕并没有反应?

这是因为我们没有为Runloop加入source(输入源),source可以是timer,port。
其中,NSPort是一个描述通信通道的抽象类,我们可以先使用port作为输入源来让Runloop持续运行,改造这个方法

- (void)selfThread {
    
    @autoreleasepool {
        [[NSRunLoop currentRunLoop] addPort:self.port forMode:NSRunLoopCommonModes];
        [[NSRunLoop currentRunLoop] run];
    }
}


现在我们就可以把需要执行的业务逻辑放入自定义的线程中执行了

还有其他许多实际的应用,比如优化空闲状体的利用、集合视图的优化等等,这里就不多叙述了

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 193,968评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,682评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,254评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,074评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,964评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,055评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,484评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,170评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,433评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,512评论 2 308
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,296评论 1 325
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,184评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,545评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,150评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,437评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,630评论 2 335

推荐阅读更多精彩内容

  • ======================= 前言 RunLoop 是 iOS 和 OSX 开发中非常基础的一个...
    i憬铭阅读 859评论 0 4
  • runtime 和 runloop 作为一个程序员进阶是必须的,也是非常重要的, 在面试过程中是经常会被问到的, ...
    SOI阅读 21,753评论 3 63
  • runtime 和 runloop 作为一个程序员进阶是必须的,也是非常重要的, 在面试过程中是经常会被问到的, ...
    made_China阅读 1,198评论 0 7
  • 转载:http://www.cocoachina.com/ios/20150601/11970.html RunL...
    Gatling阅读 1,430评论 0 13
  • 深入理解RunLoop 由ibireme| 2015-05-18 |iOS,技术 RunLoop 是 iOS 和 ...
    橙娃阅读 846评论 1 2