RxWorker


abstract class RxWorker : 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(appContext: Context, workerParams: WorkerParameters)

Public functions

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.

Single<ForegroundInfo!>

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

ListenableFuture<ForegroundInfo!>

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

Completable

Updates the progress for a RxWorker.

Completable
setForeground(foregroundInfo: ForegroundInfo)

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

Single<Void!>

This function is deprecated.

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

ListenableFuture<ListenableWorker.Result!>

Override this method to start your actual background processing.

Protected functions

Scheduler

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

Inherited functions

From androidx.work.ListenableWorker
Context

Gets the application android.content.Context.

UUID

Gets the ID of the WorkRequest that created this Worker.

Data

Gets the input data.

Network?
@RequiresApi(value = 28)
getNetwork()

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

@IntRange(from = 0) Int

Gets the current run attempt count for this work.

Int
@RequiresApi(value = 31)
getStopReason()

Returns a reason why this worker has been stopped.

(Mutable)Set<String!>

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

(Mutable)List<String!>

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

(Mutable)List<Uri!>

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

Boolean

Returns true if this Worker has been told to stop.

Unit

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

ListenableFuture<Void!>

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

ListenableFuture<Void!>

Updates ListenableWorker progress.

Public constructors

RxWorker

Added in 1.0.0
RxWorker(appContext: Context, workerParams: WorkerParameters)
Parameters
appContext: Context

The application Context

workerParams: WorkerParameters

Parameters to setup the internal state of this worker

Public functions

createWork

Added in 1.0.0
@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.

Returns
Single<ListenableWorker.Result!>

a Single<Result> that represents the work.

getForegroundInfo

Added in 2.8.0
fun getForegroundInfo(): Single<ForegroundInfo!>

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
Single<ForegroundInfo!>

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

getForegroundInfoAsync

fun getForegroundInfoAsync(): ListenableFuture<ForegroundInfo!>

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
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
fun setCompletableProgress(data: Data): Completable

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

Parameters
data: Data

The progress Data

Returns
Completable

The Completable

setForeground

Added in 2.8.0
fun setForeground(foregroundInfo: ForegroundInfo): Completable

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
foregroundInfo: ForegroundInfo

The ForegroundInfo

Returns
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
fun setProgress(data: Data): Single<Void!>

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

This method is deprecated. Use setCompletableProgress instead.

Parameters
data: Data

The progress Data

Returns
Single<Void!>

The Single

startWork

Added in 1.0.0
fun startWork(): ListenableFuture<ListenableWorker.Result!>

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
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 functions

getBackgroundScheduler

Added in 1.0.0
protected 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.

Returns
Scheduler

The default Scheduler.