2013-02-21 65 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) { 

    } 
} 
相關問題