我在iOS和macOSX两个平台上使用相同的代码进行测试,查看Runloop的活动开关,发现两个平台的结果不一样,是什么原因。
代码:
1。创建RunLoop Observer
CFRunLoopObserverContext context = {0,(__bridge void*)self, NULL, NULL, NULL};
_observer = CFRunLoopObserverCreate(kCFAllocatorDefault,
kCFRunLoopAllActivities,
YES,
0,
&runLoopObserverCallBack,
&context);
CFRunLoopAddObserver(CFRunLoopGetMain(), _observer, kCFRunLoopCommonModes);
2。打印活动状态
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;
}}
结果:
1.iOS iOS Result
2.macOS MacOS Result
为什么macOS系统中有多个kCFRunLoopEntry?