【问题标题】:Wait RxJava response等待 RxJava 响应
【发布时间】:2019-10-09 19:22:43
【问题描述】:

想想这个 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 返回 Single 你可以调用 blockingGet 等待它完成

标签: android multithreading kotlin rx-java2 kotlin-coroutines


【解决方案1】:

添加库

implementation "org.jetbrains.kotlinx:kotlinx-coroutines-rx2:$coroutine_version"

然后调用,

func1().await()

阅读更多here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-03
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-28
    相关资源
    最近更新 更多