Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

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
open ListenableFuture<ListenableWorker.Result!>

open Unit

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.

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

startWork

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

onStopped

open fun onStopped(): Unit

createWork

@MainThread 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.

Protected methods

getBackgroundScheduler

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.