RxWorker

public abstract class RxWorker
extends ListenableWorker

java.lang.Object
   ↳ androidx.work.ListenableWorker
     ↳ androidx.work.RxWorker


RxJava2 interoperability Worker implementation.

When invoked by the WorkManager, it will call @createWork() to get a Single<Payload> 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 ListenableWorker.Result. After this time has expired, the worker will be signalled to stop.

See also:

Summary

Public constructors

RxWorker(Context appContext, WorkerParameters workerParams)

Public methods

abstract Single<ListenableWorker.Result> createWork()

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

void onStopped()

This method is invoked when this Worker has been told to stop.

ListenableFuture<ListenableWorker.Result> startWork()

Override this method to start your actual background processing.

Protected methods

Scheduler getBackgroundScheduler()

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

Inherited methods

Public constructors

RxWorker

public RxWorker (Context appContext, 
                WorkerParameters workerParams)

Parameters
appContext Context: The application Context

workerParams WorkerParameters: Parameters to setup the internal state of this worker

Public methods

createWork

public abstract Single<ListenableWorker.Result> createWork ()

Override this method to define your actual work and return a Single of 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 ListenableWorker.Result. After this time has expired, the worker will be signalled to stop.

Returns
Single<ListenableWorker.Result> a Single<Result> that represents the work.

onStopped

public void onStopped ()

This method is invoked when this Worker has been told to stop. This could happen due to an explicit cancellation signal by the user, or because the system has decided to preempt the task. In these cases, the results of the work will be ignored by WorkManager. All processing in this method should be lightweight - there are no contractual guarantees about which thread will invoke this call, so this should not be a long-running or blocking operation.

startWork

public ListenableFuture<ListenableWorker.Result> startWork ()

Override this method to start your actual background processing. This method is called on the main thread.

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

Returns
ListenableFuture<ListenableWorker.Result> A ListenableFuture with the ListenableWorker.Result of the computation. If you cancel this Future, WorkManager will treat this unit of work as failed.

Protected methods

getBackgroundScheduler

protected Scheduler getBackgroundScheduler ()

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.

Returns
Scheduler The default Scheduler.