0

I'm developing a new feature that allows the user to keep track of a background upload through live activities.

Im using the URLSessionTaskDelegate to keep track of the task progress and it works fine when the app is in foreground, but when the app goes to background, it stop after a few seconds. The upload still continues and Im able to receive updates on it's completion through the application(_ application: UIApplication, handleEventsForBackgroundURLSession but only through that.

Also when I put the app back to foreground it updates to the correct progress so this is only happening in background.

This is the URLSessionConfiguration I'm using:

let config = URLSessionConfiguration.background(withIdentifier: "\(uploadBackgroundTaskIdentifier)")

config.isDiscretionary = false
config.allowsCellularAccess = true
config.shouldUseExtendedBackgroundIdleMode = true
config.sessionSendsLaunchEvents = true
config.timeoutIntervalForResource = 28800

Does anyone knows how should I be able to keep track of the progress even when the app is in background?

Yuri Frota
  • 31
  • 4

1 Answers1

0

Background url sessions are processed by the system, not your app.

When your app moves from the foreground it will execute briefly in the background before being suspended.

Once your app is suspended it will be returned to the background only to receive updates via the handleEventsForBackgroundURLSession method. You can't get more find-grained updates on the upload while suspended since your app receives no execution time.

If the upload takes less than a couple of minutes, you can use beginbackgroundTaskWithExpirarionHandler to get extended background execution time.

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • It’s worth noting that even if your upload would complete within the extended background execution duration from `beginbackgroundTaskWithExpirarionHandler`, you cannot update the state of a live activity from the background. It seems to be impossible to do what you would like to achieve. – Geoff Hackworth Jul 11 '23 at 08:41
  • The upload would take more than a few minutes, I don't think it's impossible because WeTransfer does that, its upload activity will update a live activity from time to time. I wouldn't need constant updates, once every few minutes would be enough. – Yuri Frota Jul 11 '23 at 14:11