Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resuming tasks using NSURLSession when app removed from background or on device reboot

I have checked many docs but could not found solution for Resuming tasks using NSURLSession when app removed from background or on device reboot.

I am dealing with amazon S3 to upload some files, In that I am able to

  1. Upload file to S3 using NSURLSessionUploadTask, when app is in foreground as well as in background.
  2. Resume task when app crashed due to any other reason while uploading and if app is not removed from background.
  3. Restart task if I reboot device while uploading and if app is not removed from background.

Here is my code to achive resume fuctionality written in applicationDidBecomeActive method of appdelegate.

// Initialize session config and the background session
NSURLSession *l_taskSession = [self backgroundSession];

[l_taskSession getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks)
 {
     if([uploadTasks count])
     {
         for (int i=0; i<[uploadTasks count]; i++)
         {
             NSURLSessionUploadTask *uploadRequestTask = (NSURLSessionUploadTask*)[uploadTasks objectAtIndex:i];
             [uploadRequestTask  resume];
             NSLog(@"-------- Upload Resumed ------- ");
         }
     }
     else if(![uploadTasks count])
     {
         NSLog(@"------- There are no previous tasks -------");
     }
 }];

Now the problem is in both the case (2 & 3) mentioned above it doesn't give list of tasks that were in progress, when I removed app from background and launched again, as per code it falls in else if condition and logs

2015-06-12 17:12:32.902 AppName[162:60b] ------- There are no previous tasks -------

So my question is this possible to resume tasks when app removed from background ? Or can anybody just give me reference links where I can find answer for the same, any help is appreciated.

like image 816
Ajit Satarkar Avatar asked Jun 12 '15 12:06

Ajit Satarkar


1 Answers

Finally I come to conclusion that,

If an iOS app is terminated by the system and relaunched, the app can use the same identifier to create a new configuration object and session and retrieve the status of transfers that were in progress at the time of termination. This behavior applies only for normal termination of the app by the system.

If the user terminates the app from the multitasking screen, the system cancels all of the session’s background transfers.

For more details check Apple's documentation on NSURLSessionConfiguration Class Reference here.

like image 73
Ajit Satarkar Avatar answered Nov 01 '22 16:11

Ajit Satarkar