2013-02-21 64 views
0

我正在输出调试日志,我想知道我的代码中的特定方法是否在当前runloop中执行,而不是在后续执行中。有没有办法做到这一点?有没有办法知道你当前在使用哪个runloop或frame?

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

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

您是否想要从执行线程或不同线程中查询此信息? – 2013-02-21 19:19:13

+0

让我详细说明我的问题。 – Boon 2013-02-22 02:19:25

回答

0

如果你给每个线程的名称,你可以查询。

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

检查,如果你在主runloop这样:

if ([NSRunLoop currentRunLoop] == [NSRunLoop mainRunLoop]) { 
    // ... 
} 

此测试将用于在后台线程或runloop运行的任何方法都失败了(runloops属于线程,一个存在每线程)

如果您需要确定一些代码正对一个特定的运行循环运行,缓存在一个地方有问题的runloop参考,你知道它会运行:

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

// ... later ... 

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

    } 
} 
相关问题