Home Runloop
Post
Cancel

Runloop

RunLoop流程

  1. Entry

  2. BeforeTimers

  3. BeforeSource0

  4. DoBlocks

  5. DoSource0 -> DoBlocks

  6. 如果存在Source1, 则走AfterWaiting后的逻辑

  7. BeforeWaiting

  8. AfterWaiting

    处理Timer

    服务CGD: Async to Main Queue

    处理Source1

  9. DoBlocks

  10. 根据前面的执行结果

    回到第二步

    或者

    Exit

关于Source0

处理Touch Event

performSelector:onThread:

关于Source1

基于port的线程通信

捕捉系统事件

关于Timers

NSTimer

performSelector:withObject:afterDelay:

关于 observers

监听RunLoop的状态

UI刷新(BeforeWaiting)

Autoreleas pool (当前loop BeforeWaiting时释放autorelease对象)

RunLoop数据结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
typedef struct __CFRunLoop* CFRunLoopRef;
struct __CFRunloop {
    CFRunLoopModeRef _currentMode;
    CFMutableSetRef _commonModes;
    CFMutableSetRef _commonModeItems;
    CFMutableSetRef _modes;
    CFAbsoluteTime _runtime;
    CFAbsoluteTime _sleepTime;
    CFTypeRef _counterpart;
    pthread_t _pthread;
    __CFPort _wakeUpPort; //used for CFRunLoopWakeUp
    Boolean _unused;
    pthread_mutex_t _lock // locked fro accessing mode list
}
1
2
3
4
5
6
7
8
typedef struct __CFRunLoopMode *CFRunLoopModeRef;
struct __CFRunLoopMode {
    CFStringRef _name;
    CFMutableSetRef _sources0; // CFRunLoopSourceRef
    CFMutableSetRef _sources1; // CFRunLoopSourceRef
    CFMutableArrayRef _observers;// CFRunLoopObserverRef
    CFMutableArrayRef _timers;// CFRunLoopTimerRef
}
This post is licensed under CC BY 4.0 by the author.