In the context of Live Activity, when app is launched into background due to some callback, should you wrap your work with background tasks?

I'm specifically focused on Live Activity, but I think this is somewhat a general question. The app could get a few callbacks when:

  • There's a new payload (start, update, end)
  • There's a new token (start, update)
  • There's some other lifecycle event (stale, dismissed)
  1. Assuming that the user didn't force kill the app, would the app get launched in all these scenarios?

  2. When OS launches the app for a reason, should we wrap our tasks with beginBackgroundTask or that's unnecessary if we're expecting our tasks to finish within 30 seconds? Or the OS may sometimes be under stress and give you far less time (example 3 seconds) and if you're in slow internet, then adding beginBackgroundTask may actually come in handy?

Thank you for your post. The questions you pose regarding workflow are intriguing, but I am unable to provide a comprehensive response without further information about your LiveActivity app and the specific objectives you are attempting to achieve. To assist you, I have included a few sample videos that demonstrate the setup process for LiveActivity, sourced from WWDC.

https://developer.apple.com/design/human-interface-guidelines/live-activities

Let me give it a thought based on the WWDC video: https://developer.apple.com/videos/play/wwdc2024/10068/

  • Start, Update, End: Your application can explicitly handle these events using ActivityKit or remote push notifications when Live Activities are initiated, updated, or terminated.
  • Tapping Live Activity: Tapping on a Live Activity will launch your app, potentially allowing you to handle additional logic upon launch.
  • Stale: When a Live Activity becomes stale (i.e., its defined duration elapses without updates), you can receive updates by subscribing to activityStateUpdates.
  • Dismissed by User: There is no direct callback when the user manually dismisses a Live Activity.

Background Execution:

  • Background Tasks: Your app may perform tasks in the background briefly using task completion handlers (beginBackgroundTask).

You need to be very aware about the push notifications tokens the start up and update token.

If you are interested in adopting their workflows, I recommend reviewing the documentation and samples provided.

https://developer.apple.com/videos/play/wwdc2023/10194/

Have many questions about your workflow but hopefully other developers can also provide you more opinions and resources.

Albert Pascual
  Worldwide Developer Relations.

Regardless of your use case, beginBackgroundTask might give you some additional time (up to 30 seconds), but this is not guaranteed, and if you are concerned about the times the system is under stress, then you would be unlikely be getting the full 30 seconds anyways.

While using beginBackgroundTask prophylactically is possible, this brings the additional responsibility of calling the expiration handler before the time expires, or your app process will be killed - as opposed to be merely getting suspended had you not used a background task.

If your main concern is not having enough time to do network operations, in worst case scenarios you might find that even the additional time may not be enough, and you may want to instead use a background URLSession which will hand over the download/upload to the system, and then it will happen outside of your process at a time when the system considers appropriate. If you are looking for an immediate feedback though, this would not be useful for that purpose, as when the system is under stress, it is likely that the URLSession will take place at some indeterminate time when the conditions are more suitable.

So, in the end, which methods to use will depend on your use case and the criticality of the operation you want to perform when the app gets those callbacks.


Argun Tekant /  WWDR Engineering / Core Technologies

In the context of Live Activity, when app is launched into background due to some callback, should you wrap your work with background tasks?
 
 
Q