最近,我将Kotlin Coroutines从实验版升级到1.1.1,并面临job.cancel()新版本工作方式不同的问题。
这是带有实验协同程序的代码:
fun <R : Any, T : Deferred<R>> T.runAsync(
job: Job,
onSuccess: (result: R) -> Unit,
onFailed: (errorMsg: String?) -> Unit) {
launch(UI, parent = job) {
try {
val result = this@runAsync.await()
onSuccess(result)
} catch (e: Exception) {
onFailed(e.message)
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里是1.1.1:
fun <R : Any, T : Deferred<R>> T.runAsync(
job: Job,
onSuccess: (result: R) -> Unit,
onFailed: (errorMsg: String?) -> Unit) {
GlobalScope.launch(Dispatchers.Main + job) {
try {
val result = withContext(Dispatchers.IO) { …Run Code Online (Sandbox Code Playgroud)