How to convert rxJava2's Observable to Completable?

試著忘記壹切 提交于 2019-12-04 15:33:41

问题


I have Observable stream, and I want to convert it to Completable, how I could do that?


回答1:


The fluent way is to use Observable.ignoreElements().

Observable.just(1, 2, 3)
.ignoreElements()

Convert it back via toObservable if needed.




回答2:


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.

More details can be found here in the Java doc for the method.




回答3:


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<Integer> observable = Observable.just(1, 2, 3);
Completable completable = observable.firstOrError().ignoreElement()



回答4:


Use Completable.merge(YourObservable()...




回答5:


You could use Completable.fromObservable(xx). That is worked fine on my project.



来源:https://stackoverflow.com/questions/40399397/how-to-convert-rxjava2s-observable-to-completable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!