Objective c 有没有办法知道您当前所在的运行循环或帧?

Objective c 有没有办法知道您当前所在的运行循环或帧?,objective-c,Objective C,我正在输出调试日志,我想知道代码中的特定方法是在当前运行循环中执行还是在后续运行循环中执行。有没有办法做到这一点 例如,最简单意义上的运行循环: 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++; } 如果给每个

我正在输出调试日志,我想知道代码中的特定方法是在当前运行循环中执行还是在后续运行循环中执行。有没有办法做到这一点

例如,最简单意义上的运行循环:

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++;
}

如果给每个线程一个名称,就可以查询它

NSThread *thread = [NSThread currentThread];
[thread name];

通过以下方式检查您是否在主运行循环中:

if ([NSRunLoop currentRunLoop] == [NSRunLoop mainRunLoop]) {
    // ...
}
对于在后台线程或runloop上运行的任何方法,此测试都将失败(runloop属于线程,每个线程都有一个)

如果需要确定某个特定的运行循环上是否有一段代码正在积极运行,请将相关的运行循环引用缓存在您知道它将运行的位置:

-(void)IKnowThisMethodRunsInASpecialRunLoop {
    _runLoopToWatch = [NSRunLoop currentRunLoop];
} 

// ... later ...

-(void)someMethod {
    if ([NSRunLoop currentRunLoop] == _runLoopToWatch ) {

    }
}

您想从执行线程内部还是从其他线程中查询这个问题?让我详细说明我的问题。