2012-03-13 70 views
26

bool _WebTryThreadLock(bool), 0x8053ce0: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...嘗試從主線程或Web線程以外的線程獲取Web鎖定。現在崩潰

我收到此錯誤。

今天我第一次發現了這個錯誤,顯示了密碼對話框,並且alertview顯示在頂部,直到viewWillAppear出現時才顯示視圖。這一切似乎工作得很好,當我有一天發展這一點。不知道線程鎖爲什麼會丟失,以及如何實現它?

+1

這將是很好,如果我們能夠打破這個時候發生的權利。 – 2012-12-14 10:01:37

回答

18
This may be a result of calling to UIKit from a secondary thread. Crashing now... 

......並且據我所見,這就是你正在做的。你從主線程以外的線程調用[alertProgress dismissWithClickedButtonIndex:0 animated:YES];,一個UIKit方法,這是不允許的。

對於直接回調主線程的方法如果你不能使用GCD,看看DDFoundation

你的代碼在這種情況下只會改變;

[[alertProgress dd_invokeOnMainThread] dismissWithClickedButtonIndex:0 
             animated:YES]; 
+0

但是viewcontroller和alertview和線程在這一點上應該不會運行。我應該在哪裏解僱警報視圖,我的意思是我不知道線程何時結束? – Jules 2012-03-13 07:39:43

+0

如果您的目標IOS版本允許它,最簡單的方法可能是使用GCD而不是手動線程,這將允許您_easily_指定在後臺線程完成時在主線程中執行的回調。檢查此頁面上的示例以查看完成; HTTP://en.wikipedia。org/wiki/Grand_Central_Dispatch – 2012-03-13 07:45:36

+0

需要支持較舊的iOS版本 – Jules 2012-03-13 07:51:38

1

我也有類似的問題,使用

performSelectorOnMainThread:withObject:waitUntilDone: 

來解決它。希望有所幫助。

14

我也遇到這個問題,而我是通過

[self performSelector:@selector(myMethod) withObject:nil afterDelay:.01]; 

現在調用的方法已經由主線程

[self performSelectorOnMainThread:@selector(myMethod) withObject:nil waitUntilDone:NO]; 
+0

這真是太棒了。 。 謝謝 – 2014-05-15 17:26:43

14

執行它,如果你打算做任何UI操作它解決必須在主線上完成。

只需將代碼粘貼到以下語法中

dispatch_sync(dispatch_get_main_queue(), ^{ 

//Your code goes here 

}); 

,或者您可以使用下面的語法

[self performSelectorOnMainThread:@selector(yourmethod:)withObject:obj waitUntilDone:YES] 

希望這有助於

3

//最簡單的方法是使用主線程爲調用的函數調用你的方法。

dispatch_async(dispatch_get_main_queue(), ^{ [self doSomething]; });

1

Durai答案斯威夫特3:

DispatchQueue.main.async { 
    //Your code goes here 
}