为啥 UIWebView 的 AVPlayerViewController 不在主线程上运行?

Posted

技术标签:

【中文标题】为啥 UIWebView 的 AVPlayerViewController 不在主线程上运行?【英文标题】:Why is UIWebView's AVPlayerViewController not running on the main thread?为什么 UIWebView 的 AVPlayerViewController 不在主线程上运行? 【发布时间】:2016-06-21 12:34:41 【问题描述】:

我的UIWebView 负责通过提示UIAlertController 来下载可下载的内容,如下代码所示:

    func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool 

    let nvc = self.tabBarController?.viewControllers?[1] as! UINavigationController
    let dvc = nvc.viewControllers[0] as! RCHDownloadTVC
    var isDownloadable = false
    let session = URLSession(configuration: URLSessionConfiguration.default())
    var newRequest = URLRequest(url: request.url!)
    newRequest.httpMethod = "HEAD"
    let task = session.dataTask(with: newRequest)  (data, response, error) in

        let httpResponse = response as? HTTPURLResponse

        if httpResponse?.statusCode == 200 
            for (_, mime) in self.supportedFileTypes.enumerated() 
                if response?.mimeType == mime 
                    self.showDownloadDecisionAlert(with:  (alert) in
                        self.sharedStore.addDownload(with: (request.url?.absoluteString)!)
                        dvc.reloadDownloadController()
                        self.webView.mediaPlaybackRequiresUserAction = true
                        self.webView.allowsInlineMediaPlayback = false
                        self.webView.goBack()
                        isDownloadable = true
                        , completionHandlerTwo:  (alert) in
                            self.webView.mediaPlaybackRequiresUserAction = false
                            self.webView.allowsInlineMediaPlayback = true
                            isDownloadable = false
                    )
                    break
                
            
        
    

    task.resume()

    if isDownloadable != true 
        return true
     else 
        return false
    

如果文件不可下载,completionHandlerTwo 将被执行,但问题是当completionHandlerTwo 被执行,然后 UIWebView 的本机 AVPlayerViewController 显示我收到此警告的视频

2016-06-21 14:36:14.962 DownloadAddict[1102:299310] 此应用程序正在从后台线程修改自动布局引擎,这可能导致引擎损坏和奇怪的崩溃。这将在未来的版本中导致异常。

Stack trace:(
0   CoreFoundation                      0x0000000183cb2dc8 <redacted> + 148
1   libobjc.A.dylib                     0x0000000183317f80 objc_exception_throw + 56
2   CoreFoundation                      0x0000000183cb2cf8 <redacted> + 0
3   Foundation                          0x0000000184763b2c <redacted> + 88
4   Foundation                          0x00000001845e4c3c <redacted> + 36
5   UIKit                               0x0000000188f18d98 <redacted> + 64
6   UIKit                               0x0000000188f198b0 <redacted> + 244
7   UIKit                               0x00000001896a77f0 <redacted> + 268
8   UIKit                               0x0000000189124aa0 <redacted> + 176
9   UIKit                               0x0000000188e0c1e4 <redacted> + 656
10  QuartzCore                          0x000000018679e994 <redacted> + 148
11  QuartzCore                          0x00000001867995d0 <redacted> + 292
12  QuartzCore                          0x0000000186799490 <redacted> + 32
13  QuartzCore                          0x0000000186798ac0 <redacted> + 252
14  QuartzCore                          0x0000000186798820 <redacted> + 500
15  WebCore                             0x0000000188972270 <redacted> + 176
16  WebCore                             0x0000000188934fa4 <redacted> + 368
17  CoreFoundation                      0x0000000183c6909c <redacted> + 24
18  CoreFoundation                      0x0000000183c68b30 <redacted> + 540
19  CoreFoundation                      0x0000000183c66830 <redacted> + 724
20  CoreFoundation                      0x0000000183b90c50 CFRunLoopRunSpecific + 384
21  WebCore                             0x0000000187b7e61c <redacted> + 456
22  libsystem_pthread.dylib             0x0000000183917b28 <redacted> + 156
23  libsystem_pthread.dylib             0x0000000183917a8c <redacted> + 0
24  libsystem_pthread.dylib             0x0000000183915028 thread_start + 4
)

这意味着 UI 不在主线程上工作,为什么会这样?

【问题讨论】:

【参考方案1】:

你需要打电话

dispatch_async(dispatch_get_main_queue()) ...

session.dataTask 行之后在主线程上运行它。

【讨论】:

我虽然那些非 UI 相关的东西不需要主队列,我应该在不同的地方在主队列上执行。无论如何,谢谢。

以上是关于为啥 UIWebView 的 AVPlayerViewController 不在主线程上运行?的主要内容,如果未能解决你的问题,请参考以下文章

为啥 UIWebView 不滚动?

为啥 UIWebView 的 AVPlayerViewController 不在主线程上运行?

为啥 UIWebView 不能在 Safari 中打开我的链接?

为啥我的 UIwebView 内容在 iOS 中被截断?

为啥 iOS UIWebView Video 不发送 cookie?

为啥我的 UIWebView 不显示 Javascript YouTube 播放器 iFrame?