1

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?

  • Because it has a different implementation on iOS; same reason exceptions crash iOS programs, but only abort the current runloop on macOS. These calls aren't promised to be in a particular order; it's an implementation detail, and they made lots of changes when they built it for iOS (based much more on GCD, which was fairly new at the time). But the answer is "because they're different." – Rob Napier Jul 01 '18 at 14:33
  • @RobNapier Thank you , Obviously because of their different implementations, but I want to know their different details – Stephen Lee Jul 01 '18 at 14:51
  • Those are internal implementation details. This is all the information there is: https://opensource.apple.com/source/CF/CF-1153.18/CFRunLoop.c.auto.html (only on Mac; iOS never released the source code for CoreFoundation, and Mac stopped years ago). The point is that this is not something you can rely on, and is different because the implementations are different. I don't think there's any deeper answer than that beyond people just guessing the implementations from the behaviors. If you believe it violates the docs, use bugreport.apple.com, but I don't think your question has an answer. – Rob Napier Jul 01 '18 at 15:10
  • With Hopper (https://www.hopperapp.com) you can explore a lot more of precisely what is different between the two, but there is no promise it won't change in future releases. – Rob Napier Jul 01 '18 at 15:11

0 Answers0