RxWorker


public abstract class RxWorker extends ListenableWorker


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 WorkManagerWorkers. 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.

See also
Worker

Summary

Public constructors

RxWorker(
    @NonNull Context appContext,
    @NonNull WorkerParameters workerParams
)

Public methods

abstract @NonNull 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.

@NonNull Single<ForegroundInfo>

Return an Single with an instance of ForegroundInfo if the WorkRequest is important to the user.

@NonNull ListenableFuture<ForegroundInfo>

Return an instance of ForegroundInfo if the WorkRequest is important to the user.

final @NonNull Completable

Updates the progress for a RxWorker.

final @NonNull Completable

This specifies that the WorkRequest is long-running or otherwise important.

final @NonNull Single<Void>

This method is deprecated.

This method is being deprecated because it is impossible to signal success via a `Single` type.

@NonNull ListenableFuture<ListenableWorker.Result>

Override this method to start your actual background processing.

Protected methods

@NonNull Scheduler

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

Inherited methods

From androidx.work.ListenableWorker
final @NonNull Context

Gets the application android.content.Context.

final @NonNull UUID

Gets the ID of the WorkRequest that created this Worker.

final @NonNull Data

Gets the input data.

final @Nullable Network
@RequiresApi(value = 28)
getNetwork()

Gets the android.net.Network to use for this Worker.

final @IntRange(from = 0) int

Gets the current run attempt count for this work.

final int
@RequiresApi(value = 31)
getStopReason()

Returns a reason why this worker has been stopped.

final @NonNull Set<String>

Gets a java.util.Set of tags associated with this Worker's WorkRequest.

final @NonNull List<String>

Gets the list of content authorities that caused this Worker to execute.

final @NonNull List<Uri>

Gets the list of content android.net.Uris that caused this Worker to execute.

final boolean

Returns true if this Worker has been told to stop.

void

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

final @NonNull ListenableFuture<Void>

This specifies that the WorkRequest is long-running or otherwise important.

@NonNull ListenableFuture<Void>

Updates ListenableWorker progress.

Public constructors

RxWorker

Added in 1.0.0
public RxWorker(
    @NonNull Context appContext,
    @NonNull WorkerParameters workerParams
)
Parameters
@NonNull Context appContext

The application Context

@NonNull WorkerParameters workerParams

Parameters to setup the internal state of this worker

Public methods

createWork

Added in 1.0.0
@MainThread
public abstract @NonNull Single<ListenableWorker.ResultcreateWork()

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.

Returns
@NonNull Single<ListenableWorker.Result>

a Single<Result> that represents the work.

getForegroundInfo

Added in 2.8.0
public @NonNull Single<ForegroundInfogetForegroundInfo()

Return an Single with an instance of ForegroundInfo if the WorkRequest is important to the user. In this case, WorkManager provides a signal to the OS that the process should be kept alive while this work is executing.

Prior to Android S, WorkManager manages and runs a foreground service on your behalf to execute the WorkRequest, showing the notification provided in the ForegroundInfo. To update this notification subsequently, the application can use android.app.NotificationManager.

Starting in Android S and above, WorkManager manages this WorkRequest using an immediate job.

Returns
@NonNull Single<ForegroundInfo>

A Single of ForegroundInfo instance if the WorkRequest is marked immediate. For more information look at setExpedited.

getForegroundInfoAsync

public @NonNull ListenableFuture<ForegroundInfogetForegroundInfoAsync()

Return an instance of ForegroundInfo if the WorkRequest is important to the user. In this case, WorkManager provides a signal to the OS that the process should be kept alive while this work is executing.

Prior to Android S, WorkManager manages and runs a foreground service on your behalf to execute the WorkRequest, showing the notification provided in the ForegroundInfo. To update this notification subsequently, the application can use android.app.NotificationManager.

Starting in Android S and above, WorkManager manages this WorkRequest using an immediate job.

Returns
@NonNull ListenableFuture<ForegroundInfo>

A com.google.common.util.concurrent.ListenableFuture of ForegroundInfo instance if the WorkRequest is marked immediate. For more information look at setExpedited.

setCompletableProgress

Added in 2.4.0
public final @NonNull Completable setCompletableProgress(@NonNull Data data)

Updates the progress for a RxWorker. This method returns a Completable unlike the setProgressAsync API.

Parameters
@NonNull Data data

The progress Data

setForeground

Added in 2.8.0
public final @NonNull Completable setForeground(@NonNull ForegroundInfo foregroundInfo)

This specifies that the WorkRequest is long-running or otherwise important. In this case, WorkManager provides a signal to the OS that the process should be kept alive if possible while this work is executing.

Calls to setForegroundAsync *must* complete before a RxWorker signals completion by returning a Result.

Under the hood, WorkManager manages and runs a foreground service on your behalf to execute this WorkRequest, showing the notification provided in ForegroundInfo.

Calling setForeground will fail with an IllegalStateException when the process is subject to foreground service restrictions. Consider using setExpedited and getForegroundInfo instead.

Parameters
@NonNull ForegroundInfo foregroundInfo

The ForegroundInfo

Returns
@NonNull Completable

A Completable which resolves after the RxWorker transitions to running in the context of a foreground android.app.Service.

setProgress

Added in 2.3.0
Deprecated in 2.4.0
public final @NonNull Single<VoidsetProgress(@NonNull Data data)

Updates the progress for a RxWorker. This method returns a Single unlike the setProgressAsync API.

This method is deprecated. Use setCompletableProgress instead.

Parameters
@NonNull Data data

The progress Data

Returns
@NonNull Single<Void>

The Single

startWork

Added in 1.0.0
public @NonNull ListenableFuture<ListenableWorker.ResultstartWork()

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

A ListenableWorker has a well defined execution window to to finish its execution and return a Result. After this time has expired, the worker will be signalled to stop and its com.google.common.util.concurrent.ListenableFuture will be cancelled.

The future will also be cancelled if this worker is stopped for any reason (see onStopped).

Returns
@NonNull ListenableFuture<ListenableWorker.Result>

A com.google.common.util.concurrent.ListenableFuture with the Result of the computation. If you cancel this Future, WorkManager will treat this unit of work as failed.

Protected methods

getBackgroundScheduler

Added in 1.0.0
protected @NonNull 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
@NonNull Scheduler

The default Scheduler.