Background Task Scheduling (BGAppRefreshTask)

All the nuances of when and whether a background task runs aside, does launching the app cancel the currently scheduled refresh task? As an example, consider the following case:

  • 8AM - user launches app. This launch schedules a background refresh for 12 hours later, at 8PM
  • 12PM (noon) - user launches the app, views some content, then exits the app.

Does the scheduled refresh for 8PM still exist, or does the launch at noon invalidate that task, since the refresh could conceivably be handled during that noon launch?

Hopefully this is articulated clearly enough, but I'm trying to understand the specifics of background refresh behavior, since I don't want to run that refresh every time the app is opened. However, if opening the app invalidates scheduled refreshes, I will need to include logic that will reschedule the refresh accordingly.

Answered by Engineer in 817332022

First I would like to clarify something. There is no such thing as scheduling a background task at a certain time. You usually schedule your background task, and the system will, at its discretion, dynamically determine when it is appropriate to run your task (making sure it is not run before the time you specified).

Launching the app sometime during the day may, just like many other activities the user will have on their device, influence the next time the system find it appropriate to run your task. But the ifs and whens of that is not deterministic as far as you are concerned as a developer.

Other than that, the tasks will not be canceled. you can always check if there are any pending tasks by using the getPendingTaskRequests(completionHandler:) function.

You also have the option to cancel(taskRequestWithIdentifier:) and then create a new one when your app is launched in the middle of the day and you think you wouldn't need to have a background refresh too soon after.


Argun Tekant /  DTS Engineer / Core Technologies

Accepted Answer

First I would like to clarify something. There is no such thing as scheduling a background task at a certain time. You usually schedule your background task, and the system will, at its discretion, dynamically determine when it is appropriate to run your task (making sure it is not run before the time you specified).

Launching the app sometime during the day may, just like many other activities the user will have on their device, influence the next time the system find it appropriate to run your task. But the ifs and whens of that is not deterministic as far as you are concerned as a developer.

Other than that, the tasks will not be canceled. you can always check if there are any pending tasks by using the getPendingTaskRequests(completionHandler:) function.

You also have the option to cancel(taskRequestWithIdentifier:) and then create a new one when your app is launched in the middle of the day and you think you wouldn't need to have a background refresh too soon after.


Argun Tekant /  DTS Engineer / Core Technologies

Thanks, this is helpful. And yeah, I understand that the task isn't scheduled to complete at a certain time, but rather the OS determines the appropriate time to execute the task after the specified period of time. I've included a timestamp to capture when the task last ran, so if the app opens and the background task did not execute as desired, the app launch will trigger it. I'll be sure to include logic to cancel and reschedule pending tasks as well, if the task runs on app launch.

Background Task Scheduling (BGAppRefreshTask)
 
 
Q