试图从主线程或Web线程以外的线程获取Web锁。现在崩溃了

时间:2012-03-13 07:14:32

标签: iphone objective-c ios cocoa-touch

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...

我收到了这个错误。

今天我第一次发现了这个错误,显示了密码对话框并且顶部显示了警报视图,在显示视图时,直到viewWillAppear才会显示。这一切似乎在我前几天开发时都很好用。不确定为什么线程锁已经丢失以及如何实现它?

6 个答案:

答案 0 :(得分: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]; 

答案 1 :(得分:15)

当我通过

调用方法时,我也遇到了这个问题
[self performSelector:@selector(myMethod) withObject:nil afterDelay:.01];

现在它已经通过在主线程

上执行来解决了
[self performSelectorOnMainThread:@selector(myMethod) withObject:nil waitUntilDone:NO];

答案 2 :(得分:15)

如果您要进行任何UI操作,则必须在主线程上完成..

只需将编码粘贴在以下语法

dispatch_sync(dispatch_get_main_queue(), ^{

//Your code goes here

});

或者您可以使用以下语法调用您的方法

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

希望这有帮助

答案 3 :(得分:3)

//最简单的方法是使用主线程调用该函数。

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

答案 4 :(得分:2)

Durai在Swift 3中的回答:

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

答案 5 :(得分:1)

我有类似的问题并使用了

performSelectorOnMainThread:withObject:waitUntilDone:

解决它。希望有所帮助。

相关问题