Perform Background Tasks When App Is Terminated

Perform background tasks when app is terminated

You have two options

  1. Background App Refresh
  2. Silent push notifications

Easiest one is Background App Refresh. Because later one needs a server to send the notification. You can check following API for the usage. Basically you set Background Fetch capability on Capabilities/Background Modes of your app. Then from time to time, iOS will wake up your app and call application(_:performFetchWithCompletionHandler:) delegate. You will have around 30-45 seconds to call your function and call completion handler. If you don't finish it on time, iOS will kill your app. If you don't obey the rules, iOS will give you less chances to wake up. For more detailed usage of Background Modes, you may check following tutorial

UWP in-process background task when app is closed

Turns out, contrary to the answer the condescending Microsoft employee provided, that the background task will run if the app is closed, even if it is implemented as an in-process background task.

If the background task is implemented as an in-process task, then the OnBackgroundActivated method will be used as the entry point, in case the app is not running. Otherwise, the method will be called while the app is running.

The reason why there were problems with the background task starting was that I used a condition (namely the SystemConditionType.UserPresent) on the task. This behaves very unpredictably. However, I tested (by waiting for several calls) both the in-process and out-of-process approach and the task gets run in both cases, even if the app is closed.

If a task deferral is used, the background task should not be terminated even if the user closes the app.

In conclusion, at the time of writing this post (October 17th, 2020) the documentation does not clearly explain this behavior and the aforementioned condescending misinterpretation by, ironically enough, Microsoft's own employee only proves that point.



Related Topics



Leave a reply



Submit