我对NSRunLoop循环感兴趣,尤其是对于主runloop.Via CFRunLoopObserverRef
,我们可以了解更多有关它:
CFRunLoopObserverRef observerRef = CFRunLoopObserverCreateWithHandler(NULL, kCFRunLoopAllActivities, YES, 0, ^(CFRunLoopObserverRef observer, CFRunLoopActivity activity) {
if (activity == kCFRunLoopBeforeTimers) {
weakSelf.runloopId += 1;
}
NSLog(@"RunloopId : %lu, Activity : %ld, %lf\n", (unsigned long)weakSelf.runloopId, activity, [[NSDate date] timeIntervalSince1970]);
});
CFRunLoopAddObserver(CFRunLoopGetMain(), observerRef, kCFRunLoopCommonModes);
对于主运行循环,我们可以忽略kCFRunLoopEntry
和kCFRunLoopExit
,而将重点放在kCFRunLoopBeforeTimers
,kCFRunLoopBeforeSources
,kCFRunLoopBeforeWaiting
和kCFRunLoopAfterWaiting
上。根据The Run Loop Sequence of Events和CFRunLoop.c,
kCFRunLoopBeforeTimers
,kCFRunLoopBeforeSources
,因此,我认为,当主循环进入休眠状态时,它应该处于空闲状态。
正常等待状态
使用上面的代码,当我打开应用程序(在模拟器中)并将其留在此处以使其保持空闲状态时,runloop事件回调将在控制台上打印出日志:
2016-03-08 21:58:33.522 CaptureJitterDemo[25272:1312010] RunloopId : 156, Activity : 2, 1457445513.522846
2016-03-08 21:58:33.523 CaptureJitterDemo[25272:1312010] RunloopId : 156, Activity : 4, 1457445513.522985
2016-03-08 21:58:33.523 CaptureJitterDemo[25272:1312010] RunloopId : 156, Activity : 32, 1457445513.523144
2016-03-08 21:58:49.002 CaptureJitterDemo[25272:1312010] RunloopId : 156, Activity : 64, 1457445529.002502
2016-03-08 21:58:49.002 CaptureJitterDemo[25272:1312010] RunloopId : 157, Activity : 2, 1457445529.002820
2016-03-08 21:58:49.003 CaptureJitterDemo[25272:1312010] RunloopId : 157, Activity : 4, 1457445529.003044
Activity : 32
表示beforeWaiting
,而Activity : 64
表示afterWaiting
,在这两种状态之间,主调用堆栈如下所示:它无关紧要,而是等待。
令人惊讶的等待状态
有时,我发现
beforeWaiting
和afterWaiting
之间可能会发生一些事情:- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(@"begin of url request... %lf\n", [[NSDate date] timeIntervalSince1970]);
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://c2.staticflickr.com/4/3771/9914251535_366536a515_h.jpg"]];
if (data) {
NSLog(@"end of url request : %lf\n", [[NSDate date] timeIntervalSince1970]);
}
}
当我选择一行时,控制台将输出:2016-03-08 23:00:07.927 CaptureJitterDemo[25855:1334505] RunloopId : 27, Activity : 2, 1457449207.927184
2016-03-08 23:00:07.927 CaptureJitterDemo[25855:1334505] RunloopId : 27, Activity : 4, 1457449207.927399
2016-03-08 23:00:07.927 CaptureJitterDemo[25855:1334505] RunloopId : 27, Activity : 32, 1457449207.927624
2016-03-08 23:00:07.929 CaptureJitterDemo[25855:1334505] begin of url request... 1457449207.929021
2016-03-08 23:00:10.277 CaptureJitterDemo[25855:1334505] end of url request : 1457449210.276982
2016-03-08 23:00:10.278 CaptureJitterDemo[25855:1334505] RunloopId : 27, Activity : 64, 1457449210.278046
2016-03-08 23:00:10.278 CaptureJitterDemo[25855:1334505] RunloopId : 28, Activity : 2, 1457449210.278244
2016-03-08 23:00:10.278 CaptureJitterDemo[25855:1334505] RunloopId : 28, Activity : 4, 1457449210.278486
2016-03-08 23:00:10.278 CaptureJitterDemo[25855:1334505] RunloopId : 28, Activity : 32, 1457449210.278752
根据日志,在
beforeWaiting
和afterWaiting
之间,该应用程序完成了一个url请求任务。这使我感到困惑。一开始我的误会
起初,我认为主线程上的同步url请求将导致主运行循环进入
waiting
状态。但是后来我发现该应用程序首先进入waiting
状态,然后发生了url请求。因此,现在的现象是,在主运行循环进入
waiting
状态之后,它仍然可以发出同步url请求,而无需唤醒自己-是在开玩笑吗?如果我将代码移到
viewDidAppear
:- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSLog(@"begin of url request... %lf\n", [[NSDate date] timeIntervalSince1970]);
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://c2.staticflickr.com/4/3771/9914251535_366536a515_h.jpg"]];
if (data) {
NSLog(@"end of url request : %lf\n", [[NSDate date] timeIntervalSince1970]);
}
}
控制台显示:2016-03-08 23:42:25.616 CaptureJitterDemo[26268:1377354] RunloopId : 0, Activity : 1, 1457451745.616474
2016-03-08 23:42:25.617 CaptureJitterDemo[26268:1377354] RunloopId : 1, Activity : 2, 1457451745.617201
2016-03-08 23:42:25.617 CaptureJitterDemo[26268:1377354] RunloopId : 1, Activity : 4, 1457451745.617346
2016-03-08 23:42:25.618 CaptureJitterDemo[26268:1377354] begin of url request... 1457451745.618176
2016-03-08 23:42:27.832 CaptureJitterDemo[26268:1377354] end of url request : 1457451747.832428
2016-03-08 23:42:27.832 CaptureJitterDemo[26268:1377354] RunloopId : 2, Activity : 2, 1457451747.832818
2016-03-08 23:42:27.832 CaptureJitterDemo[26268:1377354] RunloopId : 2, Activity : 4,
这似乎更可以接受。我真正想做的是
我想在主运行循环上计算循环成本的时间,但不应包括空闲时间。
循环意味着:
kCFRunLoopBeforeTimers
->kCFRunLoopBeforeSources
-> kCFRunLoopBeforeTimers
; kCFRunLoopBeforeTimers
-> kCFRunLoopBeforeSources
->kCFRunLoopBeforeWaiting
-> kCFRunLoopAfterWaiting
->kCFRunLoopBeforeTimers
; 对于
kCFRunLoopBeforeWaiting
-> kCFRunLoopAfterWaiting
之间的时间成本:它真的很闲。
因为它确实完成了工作;
谢谢你的帮助 :)
最佳答案
查看令人惊讶的等待时间堆栈跟踪,看起来Core Animation刷新是由于观察运行循环而发生的。我的猜测是,它正在观察kCFRunLoopBeforeWaiting
Activity 并最终触发didSelectRow..
。由于调用正在阻塞,因此运行循环将停留在该步骤(事件序列中的第6步),直到发出信号量为止。
要实现您想做的-为kCFRunLoopBeforeWaiting
和kCFRunLoopAfterWaiting
创建一个单独的观察者,并在CFRunLoopObserverCreateWithHandler
的第4个参数(顺序)中分别传递LONG_MAX
和0
。
另外,最好使用CACurrentMediaTime()
而不是[[NSDate date] timeIntervalSince1970]
,因为它是单调的。