Send serial task to background session iOS

Hi All,

I wanted to perform the upload task in the background using a URLSessionConfiguration background session and after that, once the upload is done, I have to send the status API call to the server again.

I have implemented the URLSession with background configuration and it is working fine and uploaded the file in the background or suspend state and invoke the app in the background with delegate.

After uploading the file, I'm sending the status call to the server but it is not working and looks like the app is invoked for a few seconds and again went to the suspend state.

Please suggest, how will I send status calls after the app invokes in the background for upload completion.

Thanks in advance.

Replies

First I should mention that the new async/await feature is not available with background URLSession. Next, I want to ask about:

After uploading the file, I'm sending the status call to the server but it is not working and looks like the app is invoked for a few seconds and again went to the suspend state.

If you are running a background URLSessionDataTask then this is also not intended to work either. Only URLSessionUploadTask and URLSessionDownloadTask should be used in a background URLSession.

If you need to upload the status of a network call, then you would need to save this status for either your foreground session when your app is no longer suspended and active, or you could investigate whether a URLSessionDataTask works with a BGAppRefreshTaskRequest. The pitfall with the BGTask side of things is that these tasks are not intended to run frequently and you should expect that there could be long gaps in-between running these tasks.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
  • Thanks, Matt for your reply. \n\n If you are running a background URLSessionDataTask then this is also not intended to work either. Only URLSessionUploadTask and URLSessionDownloadTask should be used in a background URLSession. \n\n

    ```Yes, I'm using the URLSessionDataTask but it's a default configuration, what I thought that It can be work the same way like we generally did for a silent push notification (Invoke app in the background). I also tried to add backgroundhandler block which generally waits for some time before going to suspend state. ```

    \n\n If you need to upload the status of a network call, then you would need to save this status for either your foreground session when your app is no longer suspended and active, or you could investigate whether a URLSessionDataTask works with a BGAppRefreshTaskRequest. The pitfall with the BGTask side of things is that these tasks are not intended to run frequently and you should expect that there could be long gaps in-between running these tasks.

    \n\n

    I can check for BGAppRefreshTaskRequest.```
Add a Comment

Thanks, Matt for your reply. 

If you are running a background URLSessionDataTask then this is also not intended to work either. Only URLSessionUploadTask and URLSessionDownloadTask should be used in a background URLSession. 

Yes, I'm using the URLSessionDataTask but it's a default configuration, what I thought that It can be work the same way like we generally did for a silent push notification (Invoke app in the background). I also tried to add backgroundhandler block which generally waits for some time before going to suspend state.

If you need to upload the status of a network call, then you would need to save this status for either your foreground session when your app is no longer suspended and active, or you could investigate whether a URLSessionDataTask works with a BGAppRefreshTaskRequest. The pitfall with the BGTask side of things is that these tasks are not intended to run frequently and you should expect that there could be long gaps in-between running these tasks.

I thought earlier to wait for the foreground state and update the status but the API token might expire during that time. I can check for BGAppRefreshTaskRequest.

Another option you have here would be to attempt to run your status update, or the current URLSessionDataTask as a URLSessionUploadTask instead. This would depend on the API from your server and also of course the timeliness that is expected for the status update to be completed.

For other options regarding using a background request, you may want to take a look at Quinn's article for UIApplication Background Task Notes. This is full of great information.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
  • Thanks for the reply, I will check on this.

Add a Comment

@matt, I tried using "URLSessionUploadTask" with background configuration by using method func uploadTask(with request: URLRequest, from bodyData: Data) -> URLSessionUploadTask but it gives error Terminating app due to uncaught exception 'NSGenericException', reason: 'Upload tasks from NSData are not supported in background sessions.

Please suggest.