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( |
Public methods |
|
|---|---|
abstract @NonNull Single<ListenableWorker.Result> |
Override this method to define your actual work and return a |
@NonNull Single<ForegroundInfo> |
Return an |
@NonNull ListenableFuture<ForegroundInfo> |
Return an instance of |
final @NonNull Completable |
setCompletableProgress(@NonNull Data data)Updates the progress for a |
final @NonNull Completable |
setForeground(@NonNull ForegroundInfo foregroundInfo)This specifies that the |
final @NonNull Single<Void> |
This method is deprecated. This method is being deprecated because it is impossible to signal success via a `Single |
@NonNull ListenableFuture<ListenableWorker.Result> |
Override this method to start your actual background processing. |
Protected methods |
|
|---|---|
@NonNull Scheduler |
Returns the default background scheduler that |
Inherited methods |
||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
Public methods
createWork
@MainThread
public abstract @NonNull Single<ListenableWorker.Result> createWork()
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 |
getForegroundInfo
public @NonNull Single<ForegroundInfo> getForegroundInfo()
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 |
getForegroundInfoAsync
public @NonNull ListenableFuture<ForegroundInfo> getForegroundInfoAsync()
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 |
setCompletableProgress
public final @NonNull Completable setCompletableProgress(@NonNull Data data)
Updates the progress for a RxWorker. This method returns a Completable unlike the setProgressAsync API.
| Returns | |
|---|---|
@NonNull Completable |
The |
setForeground
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 |
| Returns | |
|---|---|
@NonNull Completable |
A |
public final @NonNull Single<Void>setProgress(@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.
startWork
public @NonNull ListenableFuture<ListenableWorker.Result> startWork()
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 |
Protected methods
getBackgroundScheduler
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.