Ganesh
Ganesh

Reputation: 1919

Can NSURLSessionDownloadTask resume called twice?

I am new to NSURLSession and i did not find the answer in other stackoverflow question. So i am posting this.

I am having a Button and ProgressBar in my ViewController. Using NSURLSessionDownloadTask's instance, i am calling resume as follows

@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;

Specified above line in @interface

 NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
 NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:[NSOperationQueue mainQueue]];
 self.downloadTask = [session downloadTaskWithURL:url];

Specified above lines in @implementation and called resume method on buttonclick as follows

-(void) buttonpressed:(id)sender{
    [self.downloadTask resume];
}

Here what happens is,

When i click the button for first time, it downloads perfectly

(ie. Calling the proper delegate methods downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite & didFinishDownloadingToURL

But when i click the button again, it's not downloading (ie. Delegate methods are not calling)

1) Where i am doing mistake?

2) I want it to download again if i click the button second time. What should i do for that?

Any help appreciated, thanks for the time (:

Upvotes: 0

Views: 641

Answers (1)

danh
danh

Reputation: 62686

resume is only for suspended tasks, and yours is completed. The simple fix is to create and begin (really, resume) the task in the same function.

- (void)setupAndStartDownload {
    // your setup code, from the OP
    // then start it here
    [self.downloadTask resume];
}

-(void) buttonpressed:(id)sender{
    [self setupAndStartDownload];
}

Upvotes: 1

Related Questions