Creating a Worker
Define background work in a Worker.
Creating a Worker
The unit of background work in WorkManager is a Worker. You define the task by subclassing a worker base class and overriding its work method.
CoroutineWorker
For Kotlin, extend CoroutineWorker. Its doWork() is a suspend function, so you can call other suspend functions and run on a background dispatcher naturally.
class UploadWorker(
context: Context,
params: WorkerParameters
) : CoroutineWorker(context, params) {
override suspend fun doWork(): Result {
// background work here
return Result.success()
}
}