Why does CFRunLoopActivity behave differently in iOS and MacOS?

199 Views Asked by At

I use the same code in the iOS and macOSX two platforms to test, view the Runloop's activity switch, found that the results of the two platforms are not the same, what is the reason.s

Code:

1.create RunLoop Observer

CFRunLoopObserverContext context = {0,(__bridge void*)self, NULL, NULL, NULL};
_observer = CFRunLoopObserverCreate(kCFAllocatorDefault,
                                          kCFRunLoopAllActivities,
                                          YES,
                                          0,
                                          &runLoopObserverCallBack,
                                          &context);
CFRunLoopAddObserver(CFRunLoopGetMain(), _observer, kCFRunLoopCommonModes);

2.print activity state

static void runLoopObserverCallBack(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info){
Monitor *monitor = (__bridge Monitor*)info;

switch (activity) {
    case kCFRunLoopEntry:
        NSLog(@"activity_kCFRunLoopEntry");
        break;
    case kCFRunLoopBeforeTimers:
        NSLog(@"activity_kCFRunLoopBeforeTimers");
        break;
    case kCFRunLoopBeforeSources:
        NSLog(@"activity_kCFRunLoopBeforeSources");
        break;
    case kCFRunLoopBeforeWaiting:
        NSLog(@"activity_kCFRunLoopBeforeWaiting");
        break;
    case kCFRunLoopAfterWaiting:
        NSLog(@"activity_kCFRunLoopAfterWaiting");
        break;
    case kCFRunLoopExit:
        NSLog(@"activity_kCFRunLoopExit");
        break;
    default:
        break;
}}

The Result:

1.iOS iOS Result

2.macOS MacOS Result

Why are there multiple kCFRunLoopEntry in the macOS system?

0

There are 0 best solutions below