2
votes

I have the below implementation of 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...

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.

I do properly receive didReceiveData, didReceiveResponse callback in all scenarious .have not checked didReceiveChallenge callback though as it requires HTTPs setup here.

so , i have following questions to ask , if you can help me :

1)Why is didCompleteWithError callback not received when network is down?

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 ?

I have used cachepolicy in the request parameter while starting task. Is it because of this that didcompletewitherror is not called n instead caching delegate should be implemented??

Guys, Please help. I am stuck.

Thankyou

2
Don't touch the run loop unless the project is a command line interface without an implicit run loopvadian
what about other delegate methods? are they called when there is no breakdown in netwrok?Teja Nandamuri
ok . I got your point . But why is my delegate method not called ? could you please suggest anything ?CodeTry
@Mr.T: Yes, they are called. I forgot to add this ,havenot checked the didReceiveChallenge callback though, as it needs https setup . Others are called . I will update this.CodeTry
@user3540903 Because there could have been a failure indicated via a delegate callback that is outside the scope of the task. As you havent implemented those delegate methods, you are unaware of them. In other words it won't tell you a task failed if the whole session is broken and once it's told you that (or tried to) it won't even consider the task at all.Droppy

2 Answers

0
votes

delegateQueue :[NSOperationQueue mainQueue] instead of instead of delegateQueue :nil did the trick . I am still not sure how. But yes, the issue is gone. could anybody explain how this worked ?

0
votes

Your loop "while(!finished)" is really bad practice. 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. This might impact system behavior.

I belive removing this loop will solve your problem.