【发布时间】:2020-03-28 03:12:59
【问题描述】:
我正在尝试使用协程进行 API 调用并使用 MVVM 架构进行改造。我想在等待 API 响应就绪时显示进度条(超时 3 秒)。
在View Model我使用的是Coroutine.LiveData
class BootstrapViewModel: ViewModel() {
private val repository : ConfigRepository =
ConfigRepository()
val configurations = liveData(Dispatchers.IO) {
val retrievedConfigs = repository.getConfigurations(4)
emit(retrievedConfigs)
}
}
到目前为止,我在活动中所做的只是模拟 API 调用以更新进度条:
launch {
// simulate API call
val configFetch = async(Dispatchers.IO) {
while (progressState.value != 100) {
progressState.postValue(progressState.value?.plus(1))
delay(50)
}
}
// suspend until fetch is finished or return null in 3 sec
val result = withTimeoutOrNull(3000) { configFetch.await() }
if (result != null) {
// todo: process config... next steps
} else {
// cancel configFetch
configFetch.cancel()
// show error
}
}
我还可以观察下面的实时数据并且工作正常:
bootstrapViewModel.configurations.observe(this, Observer {
//response is ready
})
分离后一切正常。但是,当我尝试在协程范围内使用 livedata 时,事情变得一团糟。 await() 是否有协程实时数据(就像我为 configFetch 所做的那样)?
【问题讨论】:
标签: android kotlin retrofit android-livedata coroutine