RunLoop -3⃣️- CFRunLoopRunSpecific

UI事件如何响应?

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"这个打印只是为了打断点");
}
@end

上面这段代码,在NSLog处断点,调用栈如下

* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 2.1
    frame #0: 0x0000000107e9bc7d SDWebImageDemo`-[HomePageViewController touchesBegan:withEvent:](self=0x00007fb080607d90, _cmd="touchesBegan:withEvent:", touches=1 element, event=0x000060000027cb40) at HomePageViewController.m:36:5
    frame #1: 0x00007fff480bf863 UIKitCore`forwardTouchMethod + 340
    frame #2: 0x00007fff480bf6fe UIKitCore`-[UIResponder touchesBegan:withEvent:] + 49
    frame #3: 0x00007fff480ce8de UIKitCore`-[UIWindow _sendTouchesForEvent:] + 1867
    frame #4: 0x00007fff480d04c6 UIKitCore`-[UIWindow sendEvent:] + 4596
    frame #5: 0x00007fff480ab53b UIKitCore`-[UIApplication sendEvent:] + 356
    frame #6: 0x00007fff4812c71a UIKitCore`__dispatchPreprocessedEventFromEventQueue + 6847
    frame #7: 0x00007fff4812f1e0 UIKitCore`__handleEventQueueInternal + 5980
    frame #8: 0x00007fff23bd4471 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    frame #9: 0x00007fff23bd439c CoreFoundation`__CFRunLoopDoSource0 + 76
    frame #10: 0x00007fff23bd3b74 CoreFoundation`__CFRunLoopDoSources0 + 180
    frame #11: 0x00007fff23bce87f CoreFoundation`__CFRunLoopRun + 1263
    frame #12: 0x00007fff23bce066 CoreFoundation`CFRunLoopRunSpecific + 438
    frame #13: 0x00007fff384c0bb0 GraphicsServices`GSEventRunModal + 65
  * frame #14: 0x00007fff48092d4d UIKitCore`UIApplicationMain + 1621
    frame #15: 0x0000000107e9b220 SDWebImageDemo`main(argc=1, argv=0x00007ffee7d64c90) at main.m:14:16
    frame #16: 0x00007fff5227ec25 libdyld.dylib`start + 1
  • GraphicsServices -- GSEventRunModal

GraphicsServices 是一个不稳定的私有库,包含许多"mid-level" APIs:
GSCapability • GSColor • GSHeartbeat • GSEvent

  • CoreFoundation -- CFRunLoopRunSpecific

    两处调用,分别是CFRunLoopRun 和 CFRunLoopRunInMode

// DefaultMode 启动
void CFRunLoopRun(void) {   /* DOES CALLOUT */
    int32_t result;
    do {
        result = CFRunLoopRunSpecific(CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 1.0e10, false);
        CHECK_FOR_FORK();
    } while (kCFRunLoopRunStopped != result && kCFRunLoopRunFinished != result);
}

// 指定 Mode 启动 
SInt32 CFRunLoopRunInMode(CFStringRef modeName, CFTimeInterval seconds, Boolean returnAfterSourceHandled) {     /* DOES CALLOUT */
    CHECK_FOR_FORK();
    return CFRunLoopRunSpecific(CFRunLoopGetCurrent(), modeName, seconds, returnAfterSourceHandled);
}
  • CFRunLoopRunSpecific 实现细节
/**
* @param CFRunLoopRef : rl
* @param CFStringRef : modeName
* @param CFTimeInterval : seconds
* @param Boolean: returnAfterSourceHandled
*/
SInt32 CFRunLoopRunSpecific(CFRunLoopRef rl, CFStringRef modeName, CFTimeInterval seconds, Boolean returnAfterSourceHandled)
{ /* DOES CALLOUT */
    CHECK_FOR_FORK();
    if (__CFRunLoopIsDeallocating(rl))
        return kCFRunLoopRunFinished;

    __CFRunLoopLock(rl);
    CFRunLoopModeRef currentMode = __CFRunLoopFindMode(rl, modeName, false);

    //  如果currentMode为空 或者 mode里没有source/timer/observer, 直接返回。
    if (NULL == currentMode || __CFRunLoopModeIsEmpty(rl, currentMode, rl->_currentMode))
    {
        Boolean did = false;
        if (currentMode)
            __CFRunLoopModeUnlock(currentMode);
        __CFRunLoopUnlock(rl);
        return did ? kCFRunLoopRunHandledSource : kCFRunLoopRunFinished;
    }

    volatile _per_run_data *previousPerRun = __CFRunLoopPushPerRunData(rl);
    
    CFRunLoopModeRef previousMode = rl->_currentMode;
    rl->_currentMode = currentMode;
    int32_t result = kCFRunLoopRunFinished;
    // Observer 通知每次进入循环入口
    if (currentMode->_observerMask & kCFRunLoopEntry)
        __CFRunLoopDoObservers(rl, currentMode, kCFRunLoopEntry);

    // 处理Source Timer 和 
    result = __CFRunLoopRun(rl, currentMode, seconds, returnAfterSourceHandled, previousMode);

    // Observer 通知每次进入循环退出
    if (currentMode->_observerMask & kCFRunLoopExit)
        __CFRunLoopDoObservers(rl, currentMode, kCFRunLoopExit);

    __CFRunLoopModeUnlock(currentMode);
    __CFRunLoopPopPerRunData(rl, previousPerRun);
    rl->_currentMode = previousMode;
    __CFRunLoopUnlock(rl);
    return result;
}

你可能感兴趣的:(RunLoop -3⃣️- CFRunLoopRunSpecific)