Backgrounding & Workers
Backgrounding & Workers
Used for running actions in the device's background processes. All actions have a limit on how long they can run for. Depending on the device, actions can run for a max of 30 seconds up to 5 minutes.
This is all depending on the device's Watchdog (A program in the OS used to control and manage device memory).
To get started using it, you must use KmpBackgrounding.
Run your action in the background without Configuration
_4KmpBackgrounding.createAndStartWorker(null){_4 // my action to run in the background_4 // this action will run even if the device is locked_4}
Run your action in the background with Configuration
You can control whether the background task will be allowed to run depending on whether the device has sufficient storage, battery, or other. To configure this please use BackgroundOptions
Below are the options available to choose from:
_5data class BackgroundOptions(_5 val requiresInternet: Boolean = false, // whether device must be connected to internet for background process to run_5 val requiresStorage: Boolean = false, // whether device must have sufficient storage in order to run background process_5 val requiresSufficientBattery: Boolean = false // whether device must have sufficient battery in order to run background process_5)
When ready invoke the function below:
_4KmpBackgrounding.createAndStartWorker(BackgroundOptions(requiresInternet = false, requiresStorage = false, requiresSufficientBattery = false)){_4 // my action to run in the background_4 // this action will run even if the device is locked_4}
Cancel Backgrounding
If you need to cancel the background tasks, you can invoke this function. This will cancel all the registered/running background tasks above:
_1KmpBackgrounding.cancelAllRunningWorkers()
Setup for Android
For android please add this into your manifest:
_6<application xmlns:tools="http://schemas.android.com/tools">_6 <service android:name="Androidx.work.impl.foreground.SystemForegroundService"_6 android:foregroundServiceType="dataSync"_6 tools:node="merge" _6 />_6</application>
And add the below package:
_1implementation("androidx.work:work-runtime-ktx:+") // latest package
Setup for iOS
For iOS please add this into your info.plist file:
_4<key>UIBackgroundModes</key>_4<array> _4 <string>fetch</string>_4</array>