Android 科特林公司没有';I don’我等不及要做了

Android 科特林公司没有';I don’我等不及要做了,android,kotlin,kotlinx.coroutines,Android,Kotlin,Kotlinx.coroutines,我有一个遗留项目,我想在联系后端时使用协同路由。后端由Hybris提供的sdk处理。例如,它使用截击和一些回调。我想要的是用一个协程来包装这些回调。但我遇到的问题是,协程并不等待完成,它启动协程,并继续执行下一行,方法返回一个值,之后很久,协程就完成了。 我的代码: suspend fun ServiceHelper.getList(): ListOfWishes { return suspendCancellableCoroutine { continuation ->

我有一个遗留项目,我想在联系后端时使用协同路由。后端由Hybris提供的sdk处理。例如,它使用截击和一些回调。我想要的是用一个协程来包装这些回调。但我遇到的问题是,协程并不等待完成,它启动协程,并继续执行下一行,方法返回一个值,之后很久,协程就完成了。 我的代码:

suspend  fun ServiceHelper.getList(): ListOfWishes {

    return suspendCancellableCoroutine { continuation ->

        getAllLists(object : ResponseReceiver<ListOfWishes> {
            override fun onResponse(response: Response<ListOfWishes>?) {
                continuation.resume(response?.data!!)

            }

            override fun onError(response: Response<ErrorList>?) {
                val throwable = Throwable(Util.getFirstErrorSafe(response?.data))
                continuation.resumeWithException(throwable)
            }
        }, RequestUtils.generateUniqueRequestId(), false, null, object : OnRequestListener {
            override fun beforeRequest() {}
            override fun afterRequestBeforeResponse() {}
            override fun afterRequest(isDataSynced: Boolean) {}
        })
    }
}

因此,它不会等待
serviceheloper.wishLists().wait()
完成,而是返回列表。我还尝试使该方法返回
runBlocking{}
,但这只会阻塞UI线程,而不会结束协程

您可以这样做:

class Presenter : CoroutineScope { // implement CoroutineScope to create local scope
    private var job: Job = Job()

    // local coroutine context
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job

    // call this when activity or fragment is destroyed to cancel the job
    fun detachView() {
        job.cancel()
    }

    fun updateLists() = wishLists().then(this) {

        // update your UI here

        // `it` contains the ListOfWishes

    }

    private fun wishLists(): Deferred<ListOfWishes> = async(Dispatchers.IO) {
        getWishList()
    }

    private suspend  fun getWishList(): ListOfWishes = suspendCancellableCoroutine { continuation ->
        getAllLists(object : ResponseReceiver<ListOfWishes> {
            override fun onResponse(response: Response<ListOfWishes>?) {
                continuation.resume(response?.data!!)
            }

            override fun onError(response: Response<ErrorList>?) {
                val throwable = Throwable(Util.getFirstErrorSafe(response?.data))
                continuation.resumeWithException(throwable)
            }
        }, RequestUtils.generateUniqueRequestId(), false, null, object : OnRequestListener {
            override fun beforeRequest() {}
            override fun afterRequestBeforeResponse() {}
            override fun afterRequest(isDataSynced: Boolean) {}
        })
    }
}
要使用
Dispatchers.Main
将依赖项添加到应用程序的
gradle.build
文件,请执行以下操作:

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'

希望它能为您指明正确的方向。

如果您将
launch(Android)
替换为
runBlocking
,这将不会创建一个新的协同程序,它将在后台等待
愿望列表完成,而是在当前线程(可能是UI线程)中等待并返回实际的list@JPM你能说得更具体些吗?什么不起作用?你的代码没有编译?有车祸吗?
class Presenter : CoroutineScope { // implement CoroutineScope to create local scope
    private var job: Job = Job()

    // local coroutine context
    override val coroutineContext: CoroutineContext
        get() = Dispatchers.Main + job

    // call this when activity or fragment is destroyed to cancel the job
    fun detachView() {
        job.cancel()
    }

    fun updateLists() = wishLists().then(this) {

        // update your UI here

        // `it` contains the ListOfWishes

    }

    private fun wishLists(): Deferred<ListOfWishes> = async(Dispatchers.IO) {
        getWishList()
    }

    private suspend  fun getWishList(): ListOfWishes = suspendCancellableCoroutine { continuation ->
        getAllLists(object : ResponseReceiver<ListOfWishes> {
            override fun onResponse(response: Response<ListOfWishes>?) {
                continuation.resume(response?.data!!)
            }

            override fun onError(response: Response<ErrorList>?) {
                val throwable = Throwable(Util.getFirstErrorSafe(response?.data))
                continuation.resumeWithException(throwable)
            }
        }, RequestUtils.generateUniqueRequestId(), false, null, object : OnRequestListener {
            override fun beforeRequest() {}
            override fun afterRequestBeforeResponse() {}
            override fun afterRequest(isDataSynced: Boolean) {}
        })
    }
}
fun <T> Deferred<T>.then(scope: CoroutineScope = GlobalScope, uiFun: (T) -> Unit) {
    scope.launch { uiFun(this@then.await()) }
}
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1'