我正在输出调试日志,我想知道我的代码中的特定方法是否在当前的runloop和后续的runloop中执行。有没有办法实现这个目标?
例如,最简单的运行循环:
int i = 0;
while (1) {
// process event queue
// here I want to print a number
// that signifies n-th time I am processing the run loop
NSLog(@"%d", i);
i++;
}
答案 0 :(得分:0)
如果您为每个帖子命名,您可以查询它。
NSThread *thread = [NSThread currentThread];
[thread name];
答案 1 :(得分:0)
检查你是否以这种方式进入主流程:
if ([NSRunLoop currentRunLoop] == [NSRunLoop mainRunLoop]) {
// ...
}
对于在后台线程或runloop上运行的任何方法,此测试都将失败(runloops属于线程,每个线程都存在一个。)
如果您需要确定某个代码是否在特定的运行循环上正面运行,您可以在您知道它将运行的地方缓存相关的runloop引用:
-(void)IKnowThisMethodRunsInASpecialRunLoop {
_runLoopToWatch = [NSRunLoop currentRunLoop];
}
// ... later ...
-(void)someMethod {
if ([NSRunLoop currentRunLoop] == _runLoopToWatch ) {
}
}