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

为君一笑 提交于 2019-12-22 05:11:14

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/30803238/resuming-tasks-using-nsurlsession-when-app-removed-from-background-or-on-device

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!