在提交原始请求之前,我正尝试在startLoading()
内部进行单独的http调用。代码如下:
+canInitWithRequest(){
if(self.request.valueForKey("handledKey") != nil){
return false;
}
return true;
}
-startLoading(){
__block NSString * realURLString = self.request.URL.absoluteString;
//send separate http call for validation
NSMutableURLRequest validationRequest = [NSMutableURLRequest initWithURL:[URL initWithString:OUR_VALIDATION_SERVER_URL]];
{code to fill info into validationRequest, etc.}
semaphore = dispatch_semaphore_create(0); // need to sync to make sure we use the expected/real url
[NSURLProtocol setValue:YES forkey:"handledKey" inRequest: validationRequest];
[NSURLSession sendAsyncRequest:validationRequest ...completionHandler(response, data, error){
realURLString = xxxx;
print(current thread info);// line 1
dispatch_semaphore_signal(sema);
}];
print(current thread info);// line 2
dispatch_semaphore_wait(sema);
//continue original request with real url
NSMutableURLRequest realRequest = NSMutableURLRequest(realURLString);
[NSURLProtocol setValue:YES forkey:"handledKey" inRequest: realRequest];
self.connection = [NSURLSession withRequst:realRequest delegate:self];
}
}
如果在任何一个普通内容(htm, css, etc
的webView中使用,同样的代码也是有效的。)情况1或m3u8/mpg
case 2;
但是,如果直接与AVPlayer一起使用来播放相同的m3u8
案例3,则在canInitWithRequest()
方法之后,验证调用将停滞约1分钟(如果查找错误,则会出现请求超时)。并随后呈现以下请求失败。
一开始我以为是线程问题。但是第一行和第二行给出了不同的线索。
我怀疑AVPlayer使用了与webView不同的机制来处理http请求。在第二种情况下,webView可能只是覆盖了AVPlayer。但不确定。
有人能给出更多的见解吗?
谢谢!
发布于 2016-05-08 03:02:21
你很可能阻塞了主线程。startLoading()方法需要立即返回。它永远不会因为任何原因而阻塞。启动您的身份验证请求,然后返回。在信号量等待之后获取所有代码,并将其移动到完成处理程序中。
https://stackoverflow.com/questions/34206598
复制相似问题