Stepango
Stepango

Reputation: 4841

How to convert rxJava2's Observable to Completable?

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

Upvotes: 34

Views: 19089

Answers (5)

Nokuap
Nokuap

Reputation: 2379

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()

Upvotes: 4

DomonLee
DomonLee

Reputation: 193

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

Upvotes: 0

akarnokd
akarnokd

Reputation: 69997

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

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

Convert it back via toObservable if needed.

Upvotes: 99

Praveer Gupta
Praveer Gupta

Reputation: 4010

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.

Upvotes: 18

Nish
Nish

Reputation: 62

Use Completable.merge(YourObservable()...

Upvotes: 0

Related Questions