java 如何将 rxJava2 的 Observable 转换为 Completable?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40399397/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert rxJava2's Observable to Completable?
提问by Stepango
I have Observable stream, and I want to convert it to Completable, how I could do that?
我有 Observable 流,我想将其转换为 Completable,我该怎么做?
回答by akarnokd
The fluent way is to use Observable.ignoreElements()
.
流畅的方法是使用Observable.ignoreElements()
.
Observable.just(1, 2, 3)
.ignoreElements()
Convert it back via toObservable
if needed.
toObservable
如果需要,通过将其转换回来。
回答by Praveer Gupta
You can do something like below.
您可以执行以下操作。
Observable<Integer> observable = Observable.just(1, 2, 3);
Completable completable = Completable.fromObservable(observable);
Like on an Observable, you will have to subscribe to the completable
to start the asynchronous process that Observable
wraps.
就像在 Observable 上一样,您必须订阅completable
以启动Observable
包装的异步过程。
More details can be found here in the Java doc for the method.
可以在该方法的 Java 文档中找到更多详细信息。
回答by Nokuap
As I understand all this solutions will work only if Observable call onComplete
, which is not enough if you want your result Completable
to trigger after first onNext
or onError
, so for this case I'd recommend this:
据我所知,所有这些解决方案仅在 Observable 调用时才有效onComplete
,如果您希望结果Completable
在 firstonNext
或之后触发,这还不够onError
,因此对于这种情况,我建议这样做:
Observable<Integer> observable = Observable.just(1, 2, 3);
Completable completable = observable.firstOrError().ignoreElement()
回答by Nish
Use Completable.merge(YourObservable()...
使用 Completable.merge(YourObservable()...
回答by DomonLee
You could use Completable.fromObservable(xx). That is worked fine on my project.
您可以使用Completable.fromObservable(xx)。这在我的项目中运行良好。