RxJava Completable onErrorComplete + merge不起作用

时间:2018-09-03 15:41:59

标签: android kotlin rx-java rx-java2

我想合并两个Completable-s,但是没有最后的onComplete通话。

这是我的代码:

private fun dataLoading(): Completable {
    return Completable.merge(listOf(
            method1(),
            method2()))
            .doOnComplete {
                // not called
            }
}

private fun method1(): Completable {
    return merge(loadHistory(),
            loadData(),
            loadFavorites(),
            loadBalance())
            .doOnComplete {
                // called
            }
}

private fun method2(): Completable {
    return Single
            .fromFuture(locationSubject.toFuture()) // BehaviorSubject
            .timeout(1, TimeUnit.SECONDS) // waiting for coordinates 1 sec
            .flatMapCompletable { onLocationLoaded(it) } // not called
            .onErrorComplete() // got TimeoutException here
            .doOnComplete {
                // called
            }
}

如何解决?

1 个答案:

答案 0 :(得分:1)

(来自评论:)

toFuture需要完成源。使用类似这样的内容:

private fun method2(): Completable {
    return locationSubject  // BehaviorSubject
        .firstOrError() // <---------------------------------------------- Single
        .timeout(1, TimeUnit.SECONDS) // waiting for coordinates 1 sec
        .flatMapCompletable { onLocationLoaded(it) } // not called
        .onErrorComplete() // got TimeoutException here
        .doOnComplete {
            // called
        }
}