如何从块内部对mainthread执行UIKit调用

Mor*_*ess 2 multithreading objective-c uiwebview nsinvocation objective-c-blocks

我试图从回调块内推出一个视图控制器.我想要推送的视图控制器包含一个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];
             }
         }];
Run Code Online (Sandbox Code Playgroud)

错误信息:

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

我正在使用类别NSInvocation + CWVariableArguments.

yuj*_*uji 8

只需使用dispatch_get_main_queue()获取主线程然后调度到它:

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