【问题标题】:How to perform UIKit call on mainthread from inside a block如何从块内对主线程执行 UIKit 调用
【发布时间】:2012-02-23 10:33:28
【问题描述】:

我正在尝试从回调块中推送视图控制器。我要推送的视图控制器包含一个 UIWebView,它抱怨我应该在主线程上调用它。我尝试使用 NSInvocation 无济于事。如何在主线程上调用presentModalViewController:animated

[vc requestUserPermissionWithCallback:^(BOOL inGranted)
         {
             if (inGranted)
             {
                 if (serviceType == PXServiceTypeTwitter)
                 {
                     //[ad.rootViewController presentModalViewController:vc animated: YES]; // crashes
                     NSInvocation *invocation = [NSInvocation invocationWithTarget:ad.rootViewController selector:@selector(presentModalViewController:animated:) retainArguments:YES, vc, YES];
                     [invocation invokeOnMainThreadWaitUntilDone:NO]; // same result
                 }
             }
             else
             {
                 [self.navigationController popToRootViewControllerAnimated:YES];
             }
         }];

错误信息:

bool _WebTryThreadLock(bool), 0x6b21090: 试图从主线程或web线程以外的线程获取web lock。这可能是从辅助线程调用 UIKit 的结果。现在崩溃了...

我正在使用类别NSInvocation+CWVariableArguments

【问题讨论】:

    标签: objective-c multithreading uiwebview objective-c-blocks nsinvocation


    【解决方案1】:

    只需使用dispatch_get_main_queue() 获取主线程,然后分派给它:

    dispatch_async(dispatch_get_main_queue(), ^{
        // whatever code you want to run on the main thread
    });
    

    【讨论】:

      【解决方案2】:

      我有类似的问题并使用过

      performSelectorOnMainThread:withObject:waitUntilDone:
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-11
        • 2016-04-11
        • 2017-06-19
        • 1970-01-01
        • 2014-12-01
        • 1970-01-01
        相关资源
        最近更新 更多