Потоки в RxWorker
Оптимизируйте свои подборки
Сохраняйте и классифицируйте контент в соответствии со своими настройками.
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 extend RxWorker
. Finally override the RxWorker.createWork()
method to return a Single<Result>
indicating the Result
of your execution, as follows:
Котлин
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() }
}
}
Ява
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.
Контент и образцы кода на этой странице предоставлены по лицензиям. Java и OpenJDK – это зарегистрированные товарные знаки корпорации Oracle и ее аффилированных лиц.
Последнее обновление: 2025-07-29 UTC.
[null,null,["Последнее обновление: 2025-07-29 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."]]