How do I make a network call that is longer than 30 seconds?

I have a network call that can sometimes take longer than 30 seconds and user might sometimes background the app after waiting a bit (think like a image generation done on a server which takes a while). I want to make sure that I have exhausted all the options available to provide the best possible solution for the user

(1) Use beginBackgroundTask. The downside to this is that I'm only given about 30 seconds or so, and if it takes longer, the call just get killed.

(2) Use URLSessionConfiguration.background. The upside is that this can take as long as it needs but it seems to be delegated to the system, and you never know when it will run? What if the user stays in the foreground and now the user will not know when the call will even begin (determined by the OS)

(3) Use BGProcessingTask. Again problem is that we cant control when the task is run (which in this case we want it to be immediately).

So really none of the options really is ideal. Are there other options?

What I would like ideally is

  • The call should start immediately upon user request
  • The call should go on indefinitely when the app stays in foreground
  • The call should go on for an extended period (like 2 minutes) if the user puts the app in background
  • If the call is completed in the background, have a way for the app to retrieve the result when the user brings the app back in the foreground

Your analysis sounds about right to me. The only thing worth checking is point 2. Historically backgrounds sessions gave priority to requests from the foreground app, and thus such a request would start immediately. Have you actually tested that scenario?

Share and Enjoy

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

link

How do I make a network call that is longer than 30 seconds?
 
 
Q