I have Observable stream, and I want to convert it to Completable, how I could do that?
Asked
Active
Viewed 1.8k times
5 Answers
99
The fluent way is to use Observable.ignoreElements()
.
Observable.just(1, 2, 3)
.ignoreElements()
Convert it back via toObservable
if needed.

akarnokd
- 69,132
- 14
- 157
- 192
-
7More conversions can be found here. https://speakerdeck.com/jakewharton/looking-ahead-to-rxjava-2-droidcon-nyc-2016?slide=106 – Praveer Gupta Nov 04 '16 at 04:32
-
Note that RxJava 1 does not automatically convert this to a Completable. This functionality is achieved in v1 using `Observable.toCompletable()`. v1's `ignoreElements()` just creates another Observable without elements. – forresthopkinsa Aug 06 '18 at 22:59
-
flatMapCompletable can also help in this situation depending on your needs – caitcoo0odes Jun 26 '19 at 21:51
17
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.

Praveer Gupta
- 3,940
- 2
- 19
- 21
4
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()

Nokuap
- 2,289
- 2
- 17
- 17
0
You could use Completable.fromObservable(xx). That is worked fine on my project.

DomonLee
- 193
- 1
- 12