The Android Developer Challenge is back! Submit your idea before December 2.

RxWorker

abstract class RxWorker : ListenableWorker
kotlin.Any
   ↳ androidx.work.ListenableWorker
   ↳ androidx.work.RxWorker

RxJava2 interoperability Worker implementation.

When invoked by the WorkManager, it will call @createWork() to get a Single<Result> subscribe to it.

By default, RxWorker will subscribe on the thread pool that runs WorkManager Workers. You can change this behavior by overriding getBackgroundScheduler() method.

An RxWorker is given a maximum of ten minutes to finish its execution and return a androidx.work.ListenableWorker.Result. After this time has expired, the worker will be signalled to stop.

Summary

Public constructors

<init>(@NonNull appContext: Context, @NonNull workerParams: WorkerParameters)

Public methods

abstract Single<ListenableWorker.Result!>

Override this method to define your actual work and return a Single of androidx.work.ListenableWorker.Result which will be subscribed by the WorkManager.

open Unit

Single<Void!>
setProgress(@NonNull data: Data)

Updates the progress for a RxWorker.

open ListenableFuture<ListenableWorker.Result!>

Protected methods

open Scheduler

Returns the default background scheduler that RxWorker will use to subscribe.

Inherited functions

Public constructors

<init>

RxWorker(@NonNull appContext: Context, @NonNull workerParams: WorkerParameters)
Parameters
appContext Context: The application Context
workerParams Context: Parameters to setup the internal state of this worker

Public methods

createWork

@MainThread @NonNull abstract fun createWork(): Single<ListenableWorker.Result!>

Override this method to define your actual work and return a Single of androidx.work.ListenableWorker.Result which will be subscribed by the WorkManager.

If the returned Single fails, the worker will be considered as failed.

If the RxWorker is cancelled by the WorkManager (e.g. due to a constraint change), WorkManager will dispose the subscription immediately.

By default, subscription happens on the shared Worker pool. You can change it by overriding getBackgroundScheduler().

An RxWorker is given a maximum of ten minutes to finish its execution and return a androidx.work.ListenableWorker.Result. After this time has expired, the worker will be signalled to stop.

Return
Single<ListenableWorker.Result!>: a Single<Result> that represents the work.

onStopped

open fun onStopped(): Unit

setProgress

@NonNull fun setProgress(@NonNull data: Data): Single<Void!>

Updates the progress for a RxWorker. This method returns a Single unlike the ListenableWorker#setProgressAsync(Data) API.

Parameters
data Data: The progress Data
Return
Single<Void!>: The Single

startWork

@NonNull open fun startWork(): ListenableFuture<ListenableWorker.Result!>

Protected methods

getBackgroundScheduler

@NonNull protected open fun getBackgroundScheduler(): Scheduler

Returns the default background scheduler that RxWorker will use to subscribe.

The default implementation returns a Scheduler that uses the Executor which was provided in WorkManager's Configuration (or the default one it creates).

You can override this method to change the Scheduler used by RxWorker to start its subscription. It always observes the result of the Single in WorkManager's internal thread.

Return
Scheduler: The default Scheduler.