IOSBackgroundDownloadWorker

class IOSBackgroundDownloadWorker(eventStore: EventStore? = null)

Background download worker using NSURLSession's background session mechanism.

Why this exists

BGTaskScheduler tasks die with the app process (30-second iOS limit). NSURLSession background downloads are handed to an iOS OS daemon — they survive complete app termination, charge on a locked device, and hand the file back to your app when done.

Usage

val kmpWorker = IOSKmpWorker(eventStore = myStore)

// In your Swift/Compose Multiplatform UI:
kmpWorker.backgroundDownloads.download(
taskId = "user-avatar",
url = "https://cdn.example.com/avatar.jpg",
onComplete = { path -> println("Downloaded to $path") },
onError = { error -> println("Failed: $error") }
)

Required AppDelegate wiring (Swift)

func application(
_ application: UIApplication,
handleEventsForBackgroundURLSession identifier: String,
completionHandler: @escaping () -> Void
) {
IOSBackgroundDownloadWorker.handleBackgroundSession(
identifier: identifier,
completionHandler: completionHandler
)
}

Observation

Download states are emitted to TaskMonitor and persisted via EventStore:

  • TaskState.Scheduled — download enqueued with OS

  • TaskState.Success — file downloaded, path via onComplete callback

  • TaskState.Failed — network error, see onError callback

Parameters

eventStore

Optional. If provided, Success/Failed states are persisted for cold-launch replay.

Constructors

Link copied to clipboard
constructor(eventStore: EventStore? = null)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun download(taskId: String, url: String, onComplete: (localPath: String) -> Unit? = null, onError: (Exception) -> Unit? = null)

Schedules a background download via NSURLSession.