JuanDavid
JuanDavid

Reputation: 17

Chaining in RxJava

i'm having the following problem. I'm using Room and RxJava and verything goes ok but i need to chain 4 rx operations in the following order:

1 - Insert some data

2 - Querying some data

3 - With the data Queried now do another insertion

4 - Update

This is my code but it's not working.

Completable c = Completable.fromAction(() -> System.out.println("Inserting data"));
Flowable f = Flowable.fromArray(1);
Completable c1 = Completable.fromSingle((x) -> System.out.println("Inserting more data with: " + x));
Completable c2 = Completable.fromAction(() -> System.out.println("Updating"));

c.andThen(f).mergeWith(c1).mergeWith(c2).subscribe();

And this is the output

Inserting data
Inserting more data with: io.reactivex.internal.operators.completable.CompletableFromSingle$CompletableFromSingleObserver@233c0b17
Updating

It skips the second Observable

Upvotes: 0

Views: 259

Answers (1)

mol
mol

Reputation: 2677

Completable insert = Completable.fromAction(() -> System.out.println("Inserting data"));
Single<Integer> query = Single.just(1);
Completable update = Completable.fromAction(() -> System.out.println("Updating"));
Completable insertMore = query.flatMapCompletable(x ->
        Completable.fromAction(() ->
                System.out.println("Inserting more data with: " + x)
        ));

insert.andThen(insertMore).andThen(update).subscribe();

Upvotes: 1

Related Questions