1

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;
}
9to5ios
  • 5,319
  • 2
  • 37
  • 65

1 Answers1

3

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.

Hwangho Kim
  • 629
  • 4
  • 20
  • should i put it in viewdidload ? and why my [session invalidateAndCancel]; not work ? – 9to5ios May 23 '17 at 06:13
  • You can call this function at anytime in anywhere you want not only viewDidLoad and I couldn't find your code for [session invalidateAndCancel] in above. – Hwangho Kim May 23 '17 at 06:17
  • still its showing data downloads in progress in backend and its giving byte received ... – 9to5ios May 23 '17 at 06:21
  • Did you set your session as nil? Try this. – Hwangho Kim May 23 '17 at 06:26
  • If your data tasks and session is belongs to viewcontroller A and you want to go to viewController B with canceling all the downloading tasks belongs to A, then you have to cancel all this before your viewcontroller A is dismissed. Put my code for canceling in viewDidDisappear for viewController A. – Hwangho Kim May 23 '17 at 06:47
  • My download code is in view controller B not in A...i want whenever user come to view controller B , its background download task stops....if user not come to view controller B , then the download task in background keep running.... – 9to5ios May 23 '17 at 06:56
  • So your code above is for viewController B, right? In your videDidLoad it always start downloading by [dataTask resume] so that's the problem. Move this downloading start code to somewhere. – Hwangho Kim May 23 '17 at 07:20
  • 1) i am working on download manager 2) user start download multiple files...with progress bar...if he dismiss the view the download should keep working in background... 3) Now if user again visit the download view controller B, he can see the current progress continue....so i place the code in view did load ...so the current progress bar stop and it start again download...hope it help – 9to5ios May 23 '17 at 07:31
  • 1
    FYI, The session you're going to make in viewDidLoad differ from the session which is using for downloading. You should use same session to cancel or suspend. That's why I keep this session as singleton. – Hwangho Kim May 23 '17 at 23:43