简体   繁体   English

NSURLSession委托didCompleteWithError不被调用

[英]NSURLSession delegate didCompleteWithError not called

I have the below implementation of NSURLSession . 我有以下NSURLSession实现。

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration:configuration
                                                               delegate:self
                                                               delegateQueue: nil];

NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request];
[task resume];        
while(!finished) {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:100000]];
}  

and i have implemented the below delegate methds: 并且我实现了以下委托方法:

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data  
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)aresponse  
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didCompleteWithError:(NSError *)error  
- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge 
  completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition,
                         NSURLCredential *credential))completionHandler  

the "finished" variable for while loop above is set to 1 when didCompleteWithError delegate is received when indicates that there is some issue like network down, etc... 上面的while循环的“ finished”变量在收到didCompleteWithError委托时指示为1,指示存在诸如网络中断等问题。

When network is down, i don't get didCompleteWithErrorcallback , hence the while loop does not exit even though 10sec timeout has been specified and my app crashes giving Memory warning . 当网络中断时,我没有得到didCompleteWithErrorcallback ,因此即使指定了10秒超时,while循环也不会退出,并且我的应用程序崩溃并发出Memory warning

I do properly receive didReceiveData, didReceiveResponse callback in all scenarious .have not checked didReceiveChallenge callback though as it requires HTTPs setup here. 我确实在所有情况下都正确接收了didReceiveData,didReceiveResponse回调。虽然由于需要在此处设置HTTP,所以并未检查didReceiveChallenge回调。

so , i have following questions to ask , if you can help me : 因此,我有以下问题要问,如果您能帮助我的话:

1)Why is didCompleteWithError callback not received when network is down? 1)为什么在网络中断时未收到didCompleteWithError回调?

2)Considering no network issues ,is didCompleteWithError callback received on successful completion of task ?If no, what callback would indicate the completion of task , like connectionDidFinishLoading when using NSURLConnection ? 2)考虑到没有网络问题,是否在成功完成任务时收到didCompleteWithError回调?如果否,则什么回调将指示任务已完成,例如使用NSURLConnection时的connectionDidFinishLoading

I have used cachepolicy in the request parameter while starting task. 启动任务时,我已在request参数中使用cachepolicy。 Is it because of this that didcompletewitherror is not called n instead caching delegate should be implemented?? 正是由于这个原因,didcompletewitherror不被称为n而是应该实现缓存委托?

Guys, Please help. 伙计们,请帮忙。 I am stuck. 我被困住了。

Thankyou 谢谢

delegateQueue :[NSOperationQueue mainQueue] instead of instead of delegateQueue :nil did the trick . 使用委托队列:[NSOperationQueue mainQueue]而不是代替委托队列:nil做到了。 I am still not sure how. 我仍然不确定如何。 But yes, the issue is gone. 但是,是的,问题已经解决了。 could anybody explain how this worked ? 有人可以解释这是如何工作的吗?

Your loop "while(!finished)" is really bad practice. 您的循环“ while(!finished)”确实是一种不好的做法。 Basically the download task is async operation however with your while-loop you are blocking the thread and force the download task to finish in that thread. 基本上,下载任务是异步操作,但是使用while循环时,您正在阻塞线程,并强制在该线程中完成下载任务。 This might impact system behavior. 这可能会影响系统行为。

I belive removing this loop will solve your problem. 我相信删除此循环将解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM