【问题标题】:Tried to obtain the web lock from a thread other than the main thread or the web thread. Crashing now试图从除主线程或 web 线程之外的线程获取 web lock。现在崩溃
【发布时间】:2012-03-13 07:14:31
【问题描述】:

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 显示视图时才会出现。当我前几天开发这个时,这一切似乎都很好。不确定线程​​锁丢失的原因以及如何以及将如何实现它?

【问题讨论】:

  • 如果我们能在这种情况发生时立即破解就好了。

标签: iphone objective-c ios cocoa-touch


【解决方案1】:
This may be a result of calling to UIKit from a secondary thread. Crashing now...

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

如果您不能使用 GCD,要以 非常 直接的方式回调主线程,请查看 DDFoundation。

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

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

【讨论】:

  • 但是此时视图控制器和警报视图以及线程根本不应该运行。我应该在哪里关闭警报视图,我的意思是我不知道线程何时结束?
  • 如果您的目标 IOS 版本允许,最简单的方法可能是使用 GCD 而不是手动线程,这将允许您轻松指定要在后台线程完成时的主线程。检查此页面上的示例以查看是否完成; en.wikipedia.org/wiki/Grand_Central_Dispatch
  • 需要支持旧的iOS版本
  • 任何人有任何进一步的建议:(
  • @Jules 当你的线程完成后,使用 performSelectorOnMainThread 让它关闭主线程中的 alertProgress。 developer.apple.com/library/ios/#documentation/Cocoa/Reference/…:
【解决方案2】:

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

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

现在已经通过在主线程上执行解决了

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

【讨论】:

  • 这太棒了。一直在研究这个。谢谢
【解决方案3】:

如果你要做任何 UI 操作,它必须在主线程上完成..

只需将代码粘贴到以下语法中

dispatch_sync(dispatch_get_main_queue(), ^{

//Your code goes here

});

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

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

希望对你有帮助

【讨论】:

    【解决方案4】:

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

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

    【讨论】:

      【解决方案5】:

      Swift 3 中的 Durai 答案:

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

      【讨论】:

        【解决方案6】:

        我有类似的问题并使用过

        performSelectorOnMainThread:withObject:waitUntilDone:
        

        解决它。希望对您有所帮助。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-10-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-12
          相关资源
          最近更新 更多