Cancel Downloadtask pending

1.7k Views Asked by At

I have two view controller Controller A and controller B

In controller A i have a button to present view controller B.

In controller B i have a button download(to download start with below code) and a back button (dismiss view controller B to go back A)

Now i want whenever i go back from controller B to controller A, And again coming back from controller A to controller B, all pending download task to be cancel.

I try to add code, in view did load

[dataTask suspend];
[dataTask cancel];

but besides that the current running download bytes are received

  - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data

I am using below code to download multiple files

@property (nonatomic, retain) NSMutableData *dataToDownload;
@property (nonatomic) float downloadSize;
@property (nonatomic, retain)NSURLSessionDownloadTask *dataTask;
- (void)viewDidLoad {
    [super viewDidLoad];

for(int i=0;i<[urlarray count];i++)
{
    NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];

    NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: self delegateQueue: [NSOperationQueue mainQueue]];

    NSURL *url = [NSURL URLWithString: [urlarray objectAtIndex:i]];
    dataTask = [defaultSession downloadTaskWithURL: url];

    [dataTask resume];
}
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
    completionHandler(NSURLSessionResponseAllow);

    progressBar.progress=0.0f;
    _downloadSize=[response expectedContentLength];
    _dataToDownload=[[NSMutableData alloc]init];
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
    [_dataToDownload appendData:data];
    progressBar.progress=[ _dataToDownload length ]/_downloadSize;
}
1

There are 1 best solutions below

9
On

So in my memory, I canceled downloading task as below.

    NSURLSession *session = [SingletonManager sharedInstance].downloadSession;

    [session
        getTasksWithCompletionHandler:^(
            NSArray<NSURLSessionDataTask *> *_Nonnull dataTasks,
            NSArray<NSURLSessionUploadTask *> *_Nonnull uploadTasks,
            NSArray<NSURLSessionDownloadTask *> *_Nonnull downloadTasks) {

          for (NSURLSessionTask *task in downloadTasks) {
              [task cancel];
              [session invalidateAndCancel];
              NSLog(@"Download session %@ will be invalidate and cancel",
                    session);
             [SingletonManager sharedInstance].downloadSession = nil;
          }
        }];

[session invalidateAndCancel];

Means just make this session invalidate and cancel now and

[session finishTasksAndInvalidate]

Means wait until this tasks are finished then calcel all tasks.