ios 试图从主线程或 web 线程以外的线程获取 web 锁。现在崩溃
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9679754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tried to obtain the web lock from a thread other than the main thread or the web thread. Crashing now
提问by Jules
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...
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...
I'm getting this error.
我收到这个错误。
Today I've discovered this error for the first time, the password dialog is shown and the alertview is showing on top, which shouldn't occur till viewWillAppear when the view is shown. This all seemed to work fine when I developed this the other day. Not sure why the thread lock has been lost and how and were to implement it?
今天我第一次发现了这个错误,显示了密码对话框,并且警报视图显示在顶部,直到显示视图时 viewWillAppear 才会发生这种情况。当我前几天开发它时,这一切似乎都很好。不知道为什么线程锁已经丢失以及如何实现它?
采纳答案by Joachim Isaksson
This may be a result of calling to UIKit from a secondary thread. Crashing now...
...and as far as I can see that's just what you're doing. You're calling [alertProgress dismissWithClickedButtonIndex:0 animated:YES];
, a UIKit method, from a thread other than your main thread(s), which is not allowed.
...据我所知,这就是你在做什么。您正在[alertProgress dismissWithClickedButtonIndex:0 animated:YES];
从主线程以外的线程调用UIKit 方法,这是不允许的。
For a verystraight forward way to call back to the main thread if you can't use GCD, take a look at DDFoundation.
如果您不能使用 GCD,那么对于回调主线程的一种非常直接的方法,请查看DDFoundation。
Your code would in that case only change to do;
在这种情况下,您的代码只会更改为 do;
[[alertProgress dd_invokeOnMainThread] dismissWithClickedButtonIndex:0
animated:YES];
回答by kalyan711987
I too faced this issue while i was calling a method by
我在调用方法时也遇到了这个问题
[self performSelector:@selector(myMethod) withObject:nil afterDelay:.01];
Now it has solved by performing it on main thread
现在它已经通过在主线程上执行它来解决
[self performSelectorOnMainThread:@selector(myMethod) withObject:nil waitUntilDone:NO];
回答by Durai Amuthan.H
if you are going to do any UI operation it has to be done on main thread..
如果您要进行任何 UI 操作,则必须在主线程上完成。
simply paste the coding inside the following syntax
只需将编码粘贴到以下语法中
dispatch_sync(dispatch_get_main_queue(), ^{
//Your code goes here
});
or you can call your method using the following syntax
或者您可以使用以下语法调用您的方法
[self performSelectorOnMainThread:@selector(yourmethod:)withObject:obj waitUntilDone:YES]
Hope this helps
希望这可以帮助
回答by JIthin
//The easiest way is to call the function using main thread for that.
//最简单的方法是使用主线程调用该函数。
dispatch_async(dispatch_get_main_queue(), ^{
[self doSomething];
});
dispatch_async(dispatch_get_main_queue(), ^{
[self doSomething];
});
回答by MrAn3
Durai answer in Swift 3:
Durai 在 Swift 3 中的回答:
DispatchQueue.main.async {
//Your code goes here
}
回答by mgauthier
I had a similar issue and used
我有一个类似的问题并使用
performSelectorOnMainThread:withObject:waitUntilDone:
to solve it. Hope that helps.
来解决它。希望有帮助。