What is runloop? Does runloop work?

Void runloop(){int repeat = 0 do{sleep() // sleep weak_UP ()// wake up}while(repeat = 0) return 0} Ex: System events /UI render, refresh/timer/AutoReleasepoool... If there is no source/timer/observal event, the current runloop will exit immediately. 4: One runloop for each thread 5: call [NSRunloop CurrentLoop] A runloop is automatically created when there is no runloop in the threadCopy the code

Runloop Low-level (CFRunloop) interface

// CFRunLoopRef; // CFRunLoopRef; CFRunLoopTimerRef; CFRunLoopSourceRef; CFRunLoopObserverRef; CFRunLoopModeRef; struct __CFRunLoop { CFMutableSetRef _commonModes; CFMutableSetRef _commonModeItems; CFRunLoopModeRef _currentMode; CFMutableSetRef _modes; / / __CFRunLoopMode < __CFRunLoopMode > collection}; Struct __CFRunLoopMode {// handle touch event CFMutableSetRef _sources0; CFMutableSetRef _sources1; CFMutableSetRef _sources1; /// monitor the state of runloop ///UI refresh (beforewaiting) ///autoreleasepool CFMutableArrayRef _observers; //nstimer CFMutableArrayRef _timers; };Copy the code

Runloop status listener

void loopObserver(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){
    switch (activity) {
        case kCFRunLoopEntry:
            NSLog(@"kCFRunLoopEntry");
            break;
        case kCFRunLoopBeforeWaiting:
            NSLog(@"kCFRunLoopBeforeWaiting");
            break;
        case kCFRunLoopAfterWaiting:
            NSLog(@"kCFRunLoopAfterWaiting");
            break;
        case kCFRunLoopBeforeTimers:
            NSLog(@"kCFRunLoopBeforeTimers");
            break;
        case kCFRunLoopBeforeSources:
            NSLog(@"kCFRunLoopBeforeSources");
            break;
        case kCFRunLoopExit:
            NSLog(@"kCFRunLoopExit");
            break;
        default:
            break;
    }
}
CFRunLoopObserverRef obser = CFRunLoopObserverCreate(kCFAllocatorDefault, kCFRunLoopAllActivities, YES, 0, loopObserver, 0);
CFRunLoopAddObserver(CFRunLoopGetCurrent(), obser, kCFRunLoopDefaultMode);
CFRelease(obser);
Copy the code

CFRunloop Execution sequence

Runloop 2: process times 3: process sources 4: process blocks 5: Check whether sources0 exists. Process blocks 6: Check whether source1 exists. Go to step 8 7: observers, about to enter hibernation 8: observers, about to end hibernation 1:timers 2: GCD 3: dealing with blocks 9: dealing with blocks 10: Determine what to do based on the previous results. 1: Repeat step 2. 2: Exit runloop 11: Notify observers and exit runloopCopy the code