NSURLSession cancel Task

后端 未结 2 1254
离开以前
离开以前 2021-01-03 05:40

I create new NSURLSession with following configs

 if (!self.session) {
            NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundS         


        
2条回答
  •  独厮守ぢ
    2021-01-03 06:14

    I do not reccommend to use invalidateAndCancel method cause the queue and its identifier keeps invalidated and cannot be reused untill you reset the whole device.

    NSURLSession class reference

    I use this code to cancel all pending tasks.

    - (void) cancelDownloadFiles
    {
    
        [self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
    
            for (NSURLSessionTask *_task in downloadTasks)
            {
                [_task cancel];
    
                id file = [self getFileDownloadInfoIndexWithTaskIdentifier:_task.taskIdentifier];
    
                [file.downloadTask cancel];
    
                // Change all related properties.
                file.isDownloading = NO;
                file.taskIdentifier = -1;
                file.downloadProgress = 0.0;
    
            }
    
        }];
    
        cancel = YES;
    }
    

提交回复
热议问题