Performing data network request after background uploads requests complete

I am currently implementing an upload flow that utilizes a URLSession with a background configuration to allow the upload to continue running when the application is suspended or terminated by the system. When the upload has completed, and the app is launched/woken up in the backend to respond to the upload task result, I need to make an additional data request to inform the backend that the upload has completed to trigger additional work.

I am attempting to do this by making the data request and waiting for it to finish before calling the background events completion handler delivered to the AppDelegate. However, the data request never completes while in the background, but will receive a result when the app is brought to the foreground.

Often the result of this data request will be a failure: Error Domain=NSURLErrorDomain Code=-997 "Lost connection to background transfer service" or Error Domain=NSURLErrorDomain Code=-999 "canceled"

I understand that a URLSession with a background configuration will reject data tasks when the app is suspended or terminated. However, I am attempting to use a non-background configured network session for the data request while the application is running in the background, before the application is suspended again.

Is it not possible to make additional data requests when the app is launched/woken up in the background after a background upload is completed?

Replies

I am attempting to use a non-background configured network session for the data request while the application is running in the background, before the application is suspended again.

The best way to do this is to use a UIApplication background task to prevent the app from suspending until the work is done. At that point one of two things will happen:

  • The request will complete before the background task expires, and you’re done.

  • The background task expires before the request completes. You then have two options:

    • Record the work in some persistent place and try again when you next get execution time.

    • Re-issue the request in your background session.

See UIApplication Background Task Notes for lots of… ahem… background on that API.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"