Raffi Raffi - 5 months ago 32
Objective-C Question

Why is UIWebView's AVPlayerViewController not running on the main thread?

My

UIWebView
is responsible for downloading downloadable content by prompting an
UIAlertController
like in this code:

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


if the file is not downloadable the
completionHandlerTwo
will be executed but the issue is when
completionHandlerTwo
is executed and then the UIWebView's native AVPlayerViewController displays the video I am having this warning


2016-06-21 14:36:14.962 DownloadAddict[1102:299310] This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
Stack:(
0 CoreFoundation 0x0000000183cb2dc8 + 148
1 libobjc.A.dylib 0x0000000183317f80 objc_exception_throw + 56
2 CoreFoundation 0x0000000183cb2cf8 + 0
3 Foundation 0x0000000184763b2c + 88
4 Foundation 0x00000001845e4c3c + 36
5 UIKit 0x0000000188f18d98 + 64
6 UIKit 0x0000000188f198b0 + 244
7 UIKit 0x00000001896a77f0 + 268
8 UIKit 0x0000000189124aa0 + 176
9 UIKit 0x0000000188e0c1e4 + 656
10 QuartzCore 0x000000018679e994 + 148
11 QuartzCore 0x00000001867995d0 + 292
12 QuartzCore 0x0000000186799490 + 32
13 QuartzCore 0x0000000186798ac0 + 252
14 QuartzCore 0x0000000186798820 + 500
15 WebCore 0x0000000188972270 + 176
16 WebCore 0x0000000188934fa4 + 368
17 CoreFoundation 0x0000000183c6909c + 24
18 CoreFoundation 0x0000000183c68b30 + 540
19 CoreFoundation 0x0000000183c66830 + 724
20 CoreFoundation 0x0000000183b90c50 CFRunLoopRunSpecific + 384
21 WebCore 0x0000000187b7e61c + 456
22 libsystem_pthread.dylib 0x0000000183917b28 + 156
23 libsystem_pthread.dylib 0x0000000183917a8c + 0
24 libsystem_pthread.dylib 0x0000000183915028 thread_start + 4
)


which means the UI is not working on the main thread why is that happening ?

Answer

You need to call

dispatch_async(dispatch_get_main_queue()) {...}

after the session.dataTask line to run it on the main thread.

Comments