BackgroundTaskManager

Constructed-instance entry point — held by the user's app graph for the app's lifetime.

Three things hang off the instance:

  • scheduling verbs (schedule, cancel, cancelAll, scheduled, guarantees): the scheduling surface, promoted directly onto the instance. There is no separate Scheduler object to hold — pass the BackgroundTaskManager instance itself down the app graph.

  • register: associate a task id with a factory closure that builds a fresh BackgroundWorker per dispatch.

  • start: finalize init (seals the registry; iOS/macOS run the ephemeral sweep + register OS handlers + resurrect periodic schedules; Android flips the not-ready backstop). Idempotent.

  • shutdown: tear down library-owned coroutine scopes (iOS / macOS). Android is a no-op. Safe to call repeatedly.

Construct via the per-platform extension factory:

  • androidMain: BackgroundTaskManager.Companion.create taking an Application.

  • iosMain: BackgroundTaskManager.Companion.create (no required args).

  • macosMain: BackgroundTaskManager.Companion.create (no required args).

  • jvmMain: BackgroundTaskManager.Companion.create (no required args).

@OptIn(ExperimentalObjCName::class): standard SKIE annotation; stable in practice and required for boundary refinement (CLAUDE.md §8).

Types

Link copied to clipboard
object Companion

Companion object exists so per-platform source sets can install extension entry points: BackgroundTaskManager.shared (commonMain), BackgroundTaskManager.configure(application) (Android), and BackgroundTaskManager.create(...) (iOS / macOS / JVM). commonMain cannot define the constructors itself because the Android variant requires an Application and the Apple variants don't — there's no common signature that doesn't leak Any?.

Functions

Link copied to clipboard

The WorkerFactory to install in your WorkManager.Configuration. Compose with DelegatingWorkerFactory if you also use Hilt's HiltWorkerFactory or any other custom factory.

Link copied to clipboard
@ObjCName(swiftName = "cancel")
fun cancel(taskId: String): CancelOutcome

Cancel everything the library knows about for taskId:

Link copied to clipboard
@ObjCName(swiftName = "cancelAll")
fun cancelAll(): CancelOutcome

Cancel every pending scheduled request the library knows about.

Link copied to clipboard
@ObjCName(swiftName = "diagnostics")
fun diagnostics(): PlatformDiagnostics

Snapshot of environment and configuration issues the library has detected — missing iOS Info.plist entries, disabled Android WorkManager, registry not yet sealed, etc. An empty PlatformDiagnostics.diagnostics list (or PlatformDiagnostics.isHealthy == true) means the library believes the environment is correctly configured.

Link copied to clipboard
@ObjCName(swiftName = "events")
fun events(): SharedFlow<MonitorEvent>

Hot stream of MonitorEvents — every schedule, dispatch, deferral, completion, retry, cancellation, and library-internal error the scheduler observes.

Link copied to clipboard
@ObjCName(swiftName = "guarantees")
fun guarantees(): SchedulerGuarantees

What this platform's scheduler actually guarantees.

Link copied to clipboard
@ObjCName(swiftName = "register")
fun register(factory: BackgroundWorkerFactory)

Register a BackgroundWorkerFactory that owns many task ids at once. Must be called before start. Throws if start has already run, or any of the factory's BackgroundWorkerFactory.taskIds collide with an existing per-id registration or another factory.

@ObjCName(swiftName = "register")
fun register(taskId: String, factory: () -> BackgroundWorker)

Register a BackgroundWorker factory for taskId. Must be called before start. Throws if start has already run or taskId is already registered.

Link copied to clipboard
@ObjCName(swiftName = "registeredFactories")
fun registeredFactories(): List<FactoryDescriptor>

Inspector view of every registered factory — one FactoryDescriptor per closure registration and per BackgroundWorkerFactory object. See WorkerRegistry.factoryDescriptors for ordering.

Link copied to clipboard
@ObjCName(swiftName = "registeredTaskIds")
fun registeredTaskIds(): Set<String>

Every task id currently registered with the library — the union of per-id closures and every BackgroundWorkerFactory's declared ids.

Link copied to clipboard
@ObjCName(swiftName = "run")
suspend fun <R> runNow(taskId: String, task: suspend () -> R): R

Run task immediately under taskId and suspend until it completes, returning the typed result R.

Link copied to clipboard
@ObjCName(swiftName = "schedule")
fun schedule(request: WorkRequest, policy: ConflictPolicy = ConflictPolicy.Replace): ScheduleOutcome

Schedule a WorkRequest. If a request with the same WorkRequest.taskId is already pending, policy decides what happens.

Link copied to clipboard
@ObjCName(swiftName = "scheduled")
suspend fun scheduled(): List<ScheduledTask>

Snapshot of currently-scheduled (pending or running) tasks the library knows about. Best-effort per platform.

Link copied to clipboard
@ObjCName(swiftName = "shutdown")
fun shutdown()

Tear down library-owned coroutine scopes (CLAUDE.md §3 — every scope has a clear owner with a defined cancellation lifecycle).

Link copied to clipboard
@ObjCName(swiftName = "start")
fun start()

Finalize initialization. After this call: