Hi,
I'm trying to better understand how BackgroundTasks work.
From what I understand the application has two interesting states: Foreground and Background. Once the app is put in background state, all executions are paused. Once back in the foreground these executions are resumed from the moment they stopped.
Assuming I have a long task to perform that starts when the app is in the foreground, is it possible that it can pick up where it left off once the app is relaunched for a BackgroundTask?
All the indications given by Apple mention use cases where the app is awakened to do other tasks from 0, but I would like to continue something that is already in progress!
I'm having a hard time experiencing this behavior because to trigger the
e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"TASK_IDENTIFIER"]
command i have to resume the app as mentioned here.
So at that point I'm no longer sure if my process is continuing because I actually have the app in the foreground or it would run with the app in the background as well
thx
From what I understand the application has two interesting states: Foreground and Background. Once the app is put in background state, all executions are paused. Once back in the foreground these executions are resumed from the moment they stopped.
That’s not quite right. It’s way more complex, but isn’t it always (-: Here’s a short list of states that I can think of:
-
Running in the foreground
-
Running in the background
-
Suspended in the background
-
Terminated in the background, eligible for relaunch
-
Terminated in the background, ineligible for relaunch
[There’s definitely more states that don’t spring immediately to mind.]
You get into state 5 when the user removes your app from the multitasking UI.
The Background Tasks framework takes you from either states 3 or 4 to state 2.
If you get suspended while executing a long-running task, there’s a race between two events:
-
Your app being resumed, either by the user into the foreground (state 1) or by the system into the background (state 2).
-
The system terminating your app, leaving you in state 4.
If the first event wins this race, your code resumes this long-running task exactly where it left off. If the second event wins, your app gets relaunched and you are responsible for resuming the long-running task based on state that you’ve saved to disk.
Clear?
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"