Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Background Task Scheduling

Sebastian Wolf edited this page May 30, 2020 · 1 revision

ENATaskScheduler is a wrapper for the BGTaskScheduler

The app delegate handles the execution of the background tasks. On startup, it registers the bgTasks with their associated backgroundTaskSchedulerIdentifiers, which much match one of the Permitted background task scheduler identifiers entries in the app's Info.plist.

In scene delegate, we call scheduleBackgroundTaskRequests() on sceneWillResignActive(_ scene: UIScene), to schedule the bgTasks to execute after the bgTasks backgroundTaskScheduleInterval.

Note that the app delegate methods applicationWillResignActive & applicationDidEnterBackground do not get called when using scene delegates.

Note also that it is not guaranteed that these backgroound tasks will be executed at the scheduled time:

  • The time you request is advisory, which means if you request 30 minutes you can’t expect your app to be run in exactly 30 minutes.
  • It can be disabled or restricted on a per-app level, or disabled system wide if the device has entered low-power mode.
  • iOS attempts to monitor when apps are commonly launched and will attempt to adjust background fetch to match real world usage.
  • The system evaluates how you use your background fetch time and will adjust how likely your app is to run again based on what you do.

From Apple's docmentation:

"Apps that download small amounts of content quickly, and accurately reflect when they had content available to download, are more likely to receive execution time in the future than apps that take a long time to download their content or that claim content was available but then do not download anything."

So, if you abuse the system you’ll get run less often, but if you’re a good citizen then you’ll be called more often – or at least more like what you requested in the first place.

Simulated Background Task Scheduling

While an iOS device is debugging from Xcode, it will never go into background mode, which makes this somewhat difficult to test. Apple have provided a mechanism to allow for simulating scheduled background tasks, as described below:

  • Build and run
  • Background it to schedule the task
  • Pause debugger
  • Trigger a simulated launch task with identifier in Xcode's console:

e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"de.rki.coronawarnapp.exposure-notification"]

or

e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"de.rki.coronawarnapp.fetch-test-results"]

  • Unpause debugger
  • Bring app to foreground again

The app should trigger the execution of the background task (use breakpoints or logging to verify if it's working)

Console app debugging

Alternatively, while the device is connected via USB to a Mac, you can use the Console application to view logged output in realtime, by selecting the device from the right hand side.

This can be pretty verbose, and shows all console logging for all system and application processes, so filtering can be used to isolate processes or specific log message content.

Clone this wiki locally