简体   繁体   English

如何使 Kotlin 协程 function 不异步?

[英]How to make a Kotlin Coroutine function not asynchronous?

Currently, I am making API calls to 2 to 3 different APIs, where the second API Call relies on data from the first.目前,我正在对 2 到 3 个不同的 API 进行 API 调用,其中第二个 API 调用依赖于第一个调用的数据。 However, the compiler calls the 2nd function even before the first function is completed, causing an error.但是,编译器甚至在第一个 function 完成之前调用第二个 function,从而导致错误。 How can I call the 2nd function only after the first is done?如何在第一个完成后才能拨打第二个 function? Thank you谢谢

/**
     *  Function to get Bus Timings depending on Bus Service No. or Bus Stop Code
     */
    fun determineUserQuery(userInput: String) {
        // Determine if User Provided a Bus Service No. or Bus Stop Code
        val userInputResult = determineBusServiceorStop(userInput)
        viewModelScope.launch {
            if (userInputResult.busServiceBool) {
                busServiceBoolUiState = true
                coroutineScope {
                    // Provided Bus Service, Need get Route first
                    getBusRoutes(targetBusService = userInputResult.busServiceNo)
                }
                delay(2000)
                // Get the Bus Timing for Each Route
                Log.d("debug2", "String ${_busRouteUiState.value.busRouteArray}")
                getMultipleBusTimings(busRoutes = _busRouteUiState.value.busRouteArray)
            }
            else {
                // Provided Bus Stop Code
                coroutineScope {
                    launch {
                        getBusStopNames(targetBusStopCode = userInputResult.busStopCode?.toInt())
                    }
                    launch {
                        getBusTimings(userInput = userInputResult.busStopCode)
                    }
                }
            }


        }
    }

There is a method join() in CoroutineScope's Launcher, that is a completion listener. CoroutineScope 的 Launcher 中有一个方法join() ,它是一个完成监听器。

CoroutineScope(Dispatchers.Main).launch {
    CoroutineScope(Dispatchers.IO).launch {
        // Your 1st Task
        ...
    }.join()
    CoroutineScope(Dispatchers.IO).launch {
        // Your 2nd Task
        ...
    }.join()
    CoroutineScope(Dispatchers.IO).launch {
       // Your 3rd Task
        ...
    }.join()
}

There's another function async that also returns result wherever the function it gets call.. await()还有另一个 function async也会在 function 被调用的任何地方返回结果.. await()

CoroutineScope(Dispatchers.Main).launch {
    // Your 1st Task
    val result1 = CoroutineScope(Dispatchers.IO).async {
        val v1 = "Hello!"
        v1
    }
    // Your 2nd Task
    val result2 = CoroutineScope(Dispatchers.IO).async {
        val v2 = result1.await() + " Android"
        v2
    }
    // Your 3rd Task
    val result3 = CoroutineScope(Dispatchers.IO).async {
        val v3 = result2.await() + " Developer"
    }

    // execute all be calling following
    result3.await()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM