在RxJava中链接

时间:2018-05-29 00:58:35

标签: android rx-java2 rx-android

我遇到以下问题。 我正在使用Room和RxJava,一切顺利,但我需要按以下顺序链接4个rx操作:

1 - 插入一些数据

2 - 查询一些数据

3 - 现在查询数据再做一次插入

4 - 更新

这是我的代码,但它不起作用。

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

这是输出

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

跳过第二个Observable

1 个答案:

答案 0 :(得分:1)

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