我正在尝试调用API,当我的变量准备就绪时,分别更新UI组件。
这是我的网络单例,他正在启动协程:
object MapNetwork {
fun getRoute(request: RoutesRequest,
success: ((response: RoutesResponse) -> Unit)?,
fail: ((throwable: Throwable) -> Unit)? = null) {
val call = ApiClient.getInterface().getRoute(request.getURL())
GlobalScope.launch(Dispatchers.Default, CoroutineStart.DEFAULT, null, {
try {
success?.invoke(call.await())
} catch (t: Throwable) {
fail?.invoke(t)
}
})
}
}我是这样称呼它的:
network.getRoute(request,
success = {
// Make Some UI updates
},
fail = {
// handle the exception
}) 我得到了一个异常,说不能从UI线程以外的任何线程更新UI:
com.google.maps.api.android.lib6.common.apiexception.c: Not on the main thread我已经尝试过this解决方案,但是从Kotlin1.3开始,Continuation<T>类中的resume就被“弃用”了
发布于 2020-03-24 23:41:31
private var viewModelJob = Job()
private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
uiScope.launch {
withContext(Dispatchers.IO) {
//Do background tasks...
withContext(Dispatchers.Main){
//Update UI
}
}
}https://stackoverflow.com/questions/53079234
复制相似问题