Resuming tasks using NSURLSession when an application is removed from the background or when a device is rebooted

I checked a lot of documents, but could not find a solution for Resuming tasks using NSURLSession when the application is removed from the background or the device is rebooted.

I am dealing with amazon S3 to download some files. In this I can

  • Upload a file to S3 using NSURLSessionUploadTask when the application is in the foreground and also in the background.
  • Resume the task when the application crashed for some other reason at startup, and if the application is not removed from the background.
  • Restart the task if I reboot the device during boot and if the application is not removed from the background.

Here is my code to get the fuctionality summary written in the applicationDidBecome Active appdelegate method.

// 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 in both cases (2 and 3) mentioned above does not give a list of tasks to be performed, when I removed the application from the background and started it again, according to the code it falls into a different state if the condition and the logs

2015-06-12 17: 12: 32.902 AppName [162: 60b] ------- No previous tasks -------

So my question is allows you to resume tasks when you remove an application from the background? Or someone can just give me links links where I can find the answer for the same, any help is appreciated.

+6
source share
1 answer

Finally, I came to the conclusion that

If the iOS application terminates with the system and restarts, the application can use the same identifier to create a new configuration object and session and obtain the status of transfers that were in progress at the time of completion. This behavior only applies to the normal completion of the application by the system.

If the user terminates the application from the multitasking screen, the system cancels all background transfers of the sessions.

Learn more about Apple documentation at the NSURLSessionConfiguration Class link here .

+1
source

Source: https://habr.com/ru/post/989003/


All Articles