Android 等待RxJava响应

Android 等待RxJava响应,android,multithreading,kotlin,rx-java2,kotlin-coroutines,Android,Multithreading,Kotlin,Rx Java2,Kotlin Coroutines,假设func1()位于另一个java类中,您可以通过回调来访问它。我的问题是我想等待func1结果。我该怎么做 launch{ func1() func2() func3() // I want to catch the data here, so it should wait here but because of using rxjava (another thread), I cannot control it. // I know if I

假设func1()位于另一个java类中,您可以通过回调来访问它。我的问题是我想等待func1结果。我该怎么做

launch{
    func1()

    func2()
    func3()

    // I want to catch the data here, so it should wait here but because of using rxjava (another thread), I cannot control it. 
    // I know if I use new retrofit libs, I can use coroutines, but right now, I cannot change old ones. 

    //My problem is I want to wait the func1 result here. How can I do that?
}

suspend fun func2(){}
suspend fun func3(){}

//Another java class
public Single<MyData> func1(){
    apiClient.getMyData()
    .subscribe(myData -> {
        // Let's say this takes 5 seconds to get the data
    }, throwable -> {

    });
}
发布{
func1()
func2()
func3()
//我想在这里捕获数据,所以它应该在这里等待,但由于使用rxjava(另一个线程),我无法控制它。
//我知道如果我使用新的改进libs,我可以使用协同程序,但是现在,我不能改变旧的。
//我的问题是我想在这里等待func1结果。我怎么做?
}
挂起func2(){}
挂起func3(){}
//另一个java类
公共单一功能1(){
apiClient.getMyData()
.订阅(myData->{
//假设获取数据需要5秒钟
},可丢弃->{
});
}
添加库

implementation“org.jetbrains.kotlinx:kotlinx-coroutines-rx2:$coroutine\u version”

然后打电话,

func1().await()


阅读更多信息

如果
func1
返回
Single
您可以调用
blockingGet
等待它完成