Threading in RxWorker
Stay organized with collections
Save and categorize content based on your preferences.
We provide interoperability between WorkManager and RxJava. To get started,
include work-rxjava3
dependency in addition to work-runtime
in your gradle file.
There is also a work-rxjava2
dependency that supports rxjava2 instead.
Then, instead of extending Worker
, you should extendRxWorker
. Finally
override the RxWorker.createWork()
method to return a Single<Result>
indicating the Result
of your execution, as
follows:
Kotlin
class RxDownloadWorker(
context: Context,
params: WorkerParameters
) : RxWorker(context, params) {
override fun createWork(): Single<Result> {
return Observable.range(0, 100)
.flatMap { download("https://www.example.com") }
.toList()
.map { Result.success() }
}
}
Java
public class RxDownloadWorker extends RxWorker {
public RxDownloadWorker(Context context, WorkerParameters params) {
super(context, params);
}
@NonNull
@Override
public Single<Result> createWork() {
return Observable.range(0, 100)
.flatMap { download("https://www.example.com") }
.toList()
.map { Result.success() };
}
}
Note that RxWorker.createWork()
is called on the main thread, but the return
value is subscribed on a background thread by default. You can override RxWorker.getBackgroundScheduler()
to change the
subscribing thread.
When an RxWorker
is onStopped()
, the subscription will get disposed of, so
you don't need to handle work stoppages in any special way.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2024-01-03 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-01-03 UTC."],[],[],null,["# Threading in RxWorker\n\nWe provide interoperability between WorkManager and RxJava. To get started,\ninclude [`work-rxjava3` dependency in addition to `work-runtime`](/jetpack/androidx/releases/work#declaring_dependencies) in your gradle file.\nThere is also a `work-rxjava2` dependency that supports rxjava2 instead.\n\nThen, instead of extending `Worker`, you should extend`RxWorker`. Finally\noverride the [`RxWorker.createWork()`](/reference/androidx/work/RxWorker#createWork())\nmethod to return a `Single\u003cResult\u003e` indicating the [`Result`](/reference/androidx/work/ListenableWorker.Result) of your execution, as\nfollows: \n\n### Kotlin\n\n```kotlin\nclass RxDownloadWorker(\n context: Context,\n params: WorkerParameters\n) : RxWorker(context, params) {\n override fun createWork(): Single\u003cResult\u003e {\n return Observable.range(0, 100)\n .flatMap { download(\"https://www.example.com\") }\n .toList()\n .map { Result.success() }\n }\n}\n```\n\n### Java\n\n```java\npublic class RxDownloadWorker extends RxWorker {\n\n public RxDownloadWorker(Context context, WorkerParameters params) {\n super(context, params);\n }\n\n @NonNull\n @Override\n public Single\u003cResult\u003e createWork() {\n return Observable.range(0, 100)\n .flatMap { download(\"https://www.example.com\") }\n .toList()\n .map { Result.success() };\n }\n}\n```\n\nNote that `RxWorker.createWork()` is *called* on the main thread, but the return\nvalue is *subscribed* on a background thread by default. You can override [`RxWorker.getBackgroundScheduler()`](/reference/androidx/work/RxWorker#getBackgroundScheduler()) to change the\nsubscribing thread.\n\nWhen an `RxWorker` is `onStopped()`, the subscription will get disposed of, so\nyou don't need to handle [work stoppages](/topic/libraries/architecture/workmanager/how-to/managing-work#cancelling) in any special way."]]