RxWorker
abstract class RxWorker : ListenableWorker
RxJava3 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<ForegroundInfo!> |
Return an |
ListenableFuture<ForegroundInfo!> |
Return an instance of |
Completable |
setCompletableProgress(data: Data)Updates the progress for a |
Completable |
setForeground(foregroundInfo: ForegroundInfo)This specifies that the |
ListenableFuture<ListenableWorker.Result!> |
Override this method to start your actual background processing. |
Protected functions |
|
|---|---|
Scheduler |
Returns the default background scheduler that |
Inherited functions |
||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
Public constructors
RxWorker
RxWorker(appContext: Context, workerParams: WorkerParameters)
| Parameters | |
|---|---|
appContext: Context |
The application |
workerParams: WorkerParameters |
Parameters to setup the internal state of this worker |
Public functions
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.
| Returns | |
|---|---|
Single<ListenableWorker.Result!> |
a |
getForegroundInfo
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 |
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 |
setCompletableProgress
fun setCompletableProgress(data: Data): Completable
Updates the progress for a RxWorker. This method returns a Completable similar to the setProgressAsync API.
| Returns | |
|---|---|
Completable |
The |
setForeground
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 |
| Returns | |
|---|---|
Completable |
A |
startWork
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 |
Protected functions
getBackgroundScheduler
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.