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

Jul*_*les 27 iphone cocoa-touch objective-c ios

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

我收到了这个错误.

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

Joa*_*son 19

This may be a result of calling to UIKit from a secondary thread. Crashing now...
Run Code Online (Sandbox Code Playgroud)

......据我所知,这就是你正在做的事情.您正在[alertProgress dismissWithClickedButtonIndex:0 animated:YES];从主线程以外的线程调用UIKit方法,这是不允许的.

对于一个非常直接的方式来调用回主线程,如果你不能使用GCD,看看DDFoundation.

在这种情况下,您的代码只会更改;

[[alertProgress dd_invokeOnMainThread] dismissWithClickedButtonIndex:0 
                                       animated:YES]; 
Run Code Online (Sandbox Code Playgroud)

  • @Jules当你的线程完成后,使用performSelectorOnMainThread让它解除主线程中的alertProgress.https://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/nsobject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelectorOnMainThread:withObject:waitUntilDone: (2认同)

小智 15

当我在调用方法时,我也遇到了这个问题

[self performSelector:@selector(myMethod) withObject:nil afterDelay:.01];
Run Code Online (Sandbox Code Playgroud)

现在它通过在主线程上执行它来解决

[self performSelectorOnMainThread:@selector(myMethod) withObject:nil waitUntilDone:NO];
Run Code Online (Sandbox Code Playgroud)


Dur*_*n.H 15

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

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

dispatch_sync(dispatch_get_main_queue(), ^{

//Your code goes here

});
Run Code Online (Sandbox Code Playgroud)

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

[self performSelectorOnMainThread:@selector(yourmethod:)withObject:obj waitUntilDone:YES]
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助