Using background URL sessions with background fetch

Hello,


I am trying to implement a periodic background fetch using a URL Session with a background configuration. However, I am unsure how I should handle the completion callback. The API spec says I must call it after the transfer is complete. This, however, completely defeats the purpose of the background download. Indeed, the documentation says that the app has 30 seconds of wall clock time to call the callback, after which it is terminated, but background downloads started in a background app state have essentially no guarantee of when they are actually executed, rendering the entire mechanism all but useless.


My question is this: After starting a background download from -application:performFetchWithCompletionHandler:, when should I call the completion handler and with what argument? My gut tells me to call it straight away, always with UIBackgroundFetchResultNewData, but it does not feel entirely right. It seems doubly strange that both of these features were added in the same iOS version, yet their designs are incompatible with each other.


-Pertti

Answered by DTS Engineer in 124086022

My question is this: After starting a background download from -application:performFetchWithCompletionHandler:, when should I call the completion handler and with what argument?

Once you have queued the request in the NSURLSession background session. In response to calling the completion handler the system will:

  • snapshot your UI so that its multitasking tile reflects the ‘updating’ state

  • allow your app to suspend

When the NSURLSession request completes you’ll get a second completion handler via

-application:handleEventsForBackgroundURLSession:completionHandler:
. You should call that once you’ve finished processing the results of the request. The system will then re-snapshot your UI, this time reflecting the final results.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

My question is this: After starting a background download from -application:performFetchWithCompletionHandler:, when should I call the completion handler and with what argument?

Once you have queued the request in the NSURLSession background session. In response to calling the completion handler the system will:

  • snapshot your UI so that its multitasking tile reflects the ‘updating’ state

  • allow your app to suspend

When the NSURLSession request completes you’ll get a second completion handler via

-application:handleEventsForBackgroundURLSession:completionHandler:
. You should call that once you’ve finished processing the results of the request. The system will then re-snapshot your UI, this time reflecting the final results.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Using background URL sessions with background fetch
 
 
Q