如何进行多个异步 (RX) 调用,并等待它们在 Kotlin 中完成?

How to make several async (RX) calls, and wait for them to finish in Kotlin?

我正在尝试找到一种方法来 运行 在 3 个异步 rx-fun 完成后编写一些代码。

有谁知道使用 RX 的好方法吗?

我对这个主题很陌生,没有任何 运行 可用的代码可以展示,但我可以说我现在解决代码中问题的方法是设置 3 个布尔值在每个乐趣的异步部分完成后变为真,然后在我正在等待的代码中,我正在 运行 宁我订阅的第 4 个函数(RX Flowable)检查所有 3 个布尔值是否为真。

看起来有点像这样:

// var async1IsDoneBoolean = false
// var async2IsDoneBoolean = false
// var async3IsDoneBoolean = false

fun async1() {
    // Start async work {
    // working..
    // done!
    // async1IsDoneBoolean = true
    // }
}
fun async2() {
    // Start async work {
    // working..
    // done!
    // async2IsDoneBoolean = true
    // }
}
fun async3() {
    // Start async work {
    // working..
    // done!
    // async3IsDoneBoolean = true
    // }
}

fun useResulfOfAsyncFuns() {
    // Create and subscribe to RX Flowable (will repeat until unsubscribed)
    // if (async1IsDoneBoolean && async2IsDoneBoolean && async3IsDoneBoolean) {
    // Run code after all async is done
    // }
}

 main() {
    async1()
    async2()
    async3()
    useResulfOfAsyncFuns()
 }

您可以将 Completable.merge 与各种方法一起使用 运行 async via subscribeOn:

Completable.mergeArray(
   Completable.fromAction { async1() }.subscribeOn(Schedulers.io()),
   Completable.fromAction { async2() }.subscribeOn(Schedulers.io()),
   Completable.fromAction { async3() }.subscribeOn(Schedulers.io())
)
.andThen(Flowable...)