Ash
Ash

Reputation: 2294

Retrying a failed download in URLSession:task:didCompleteWithError

How do I resume the download task after a delay?

I had a look at this thread:

iOS NSURLSession, how to retry in didCompleteWithError

The third approach suggested by Rob is what I'm trying to do, but I don't know how to set a delay.

So far, I've got something like this:

-(void)URLSession:(NSURLSession*)session task:(NSURLSessionTask*)task didCompleteWithError:(NSError*)error {
  if ( error == nil )
    return;

  NSData* resume_data = error.userInfo[NSURLSessionDownloadTaskResumeData];
  NSURLSessionDownloadTask* new_task = [_session downloadTaskWithResumeData:resume_data];  
  [new_task resume];
}

I'd like to wait 10 seconds (say) and then resume.

Upvotes: 1

Views: 1669

Answers (2)

dgatwood
dgatwood

Reputation: 10407

You should not resume it after a delay. That will just lead to your app using lots of resources when there's no network connection.

Instead, use reachability to determine when it is appropriate to try again. Create a reachability object for the exact host from the original URL, and when that reachability object says that the host is reachable, retry the request.

Upvotes: 3

danh
danh

Reputation: 62676

If [new_task resume] works, then so will:

[new_task performSelector:@selector(resume) withObject:nil afterDelay:10.0]

Upvotes: 1

Related Questions