【发布时间】:2020-05-11 08:55:03
【问题描述】:
我想将现有的 Android 应用迁移到最近的协程版本。任何人都可以帮助重写这个 Kotline 协程函数。我需要这些函数 launchAsync async 和 asyncAwait 函数,正如它在我的视图模型中描述的那样。
package com.xx.xxx.xxxx.
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.CoroutineScope
import kotlinx.coroutines.experimental.Deferred
import kotlinx.coroutines.experimental.Job
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.launch
fun launchAsync(block: suspend CoroutineScope.() -> Unit): Job {
return launch(UI) { block() }
}
suspend fun <T> async(block: suspend CoroutineScope.() -> T): Deferred<T> {
return async(CommonPool) { block() }
}
suspend fun <T> asyncAwait(block: suspend CoroutineScope.() -> T): T {
return async(block).await()
}
谢谢
【问题讨论】:
标签: android kotlin mvvm kotlin-coroutines