RunLoop的基本运行逻辑

一、基本逻辑描述:

1、通知Observer:进入Loop
2、通知Observer:即将处理Timers
3、通知Observer:即将处理Sources
4、处理Blocks
5、处理Source0(中间可能会处理Blocks)
6、如果存在Source1,跳转到第8步
7、通知Observer,开始休眠 (等待唤醒)
8、通知Observer,结束休眠 (被唤醒)
8.1、处理Timer
8.2、处理GCD Async To Main Queur
8.3、处理Source1
9、处理Blocks
10、根据前面的执行结果,决定如何操作
10.1、回到第2步
10.2、退出Loop
11、通知Observer,退出Loop

二、添加Observer对象观察RunLoop状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#import "ViewController.h"
#import <objc/runtime.h>
 
@implementation ViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    CFRunLoopAddObserver(CFRunLoopGetMain(), CFRunLoopObserverCreateWithHandler(kCFAllocatorDefault, kCFRunLoopAllActivities, true, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
        switch (activity) {
            case kCFRunLoopEntry:
                NSLog(@"kCFRunLoopEntry");
                break;
            case kCFRunLoopBeforeTimers:
                NSLog(@"kCFRunLoopBeforeTimers");
                break;
            case kCFRunLoopBeforeSources:
                NSLog(@"kCFRunLoopBeforeSources");
                break;
            case kCFRunLoopBeforeWaiting:
                NSLog(@"kCFRunLoopBeforeWaiting");
                break;
            case kCFRunLoopAfterWaiting:
                NSLog(@"kCFRunLoopAfterWaiting");
                break;
            case kCFRunLoopExit:
                NSLog(@"kCFRunLoopExit");
                break;
            default:
                break;
        }
    }), kCFRunLoopDefaultMode);
}
 
@end

运行结果:
2018-11-23 13:00:26.772786+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:26.772945+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:26.773104+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:26.773196+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:26.773359+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:26.773446+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:26.774171+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:26.774318+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:26.774703+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:26.774982+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:26.775247+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:26.775529+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:26.776696+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:26.777218+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:26.777738+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:26.778066+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:26.778600+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:26.778890+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:26.779473+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:26.779768+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:26.780083+0800 runtime[5318:1718588] kCFRunLoopBeforeWaiting
2018-11-23 13:00:27.206960+0800 runtime[5318:1718588] kCFRunLoopAfterWaiting
2018-11-23 13:00:27.207376+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:27.207481+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:27.207573+0800 runtime[5318:1718588] kCFRunLoopBeforeWaiting
2018-11-23 13:00:27.207710+0800 runtime[5318:1718588] kCFRunLoopAfterWaiting
2018-11-23 13:00:27.207817+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:27.207917+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:27.208026+0800 runtime[5318:1718588] kCFRunLoopBeforeWaiting
2018-11-23 13:00:28.161836+0800 runtime[5318:1718588] kCFRunLoopAfterWaiting
2018-11-23 13:00:28.162695+0800 runtime[5318:1718588] kCFRunLoopBeforeTimers
2018-11-23 13:00:28.162891+0800 runtime[5318:1718588] kCFRunLoopBeforeSources
2018-11-23 13:00:28.163063+0800 runtime[5318:1718588] kCFRunLoopBeforeWaiting

Leave a Reply