- 列表项
我正在进行一个简单的网络调用,当网络无法向用户显示适当的消息时,我从拦截器抛出了一个异常。当我将调用建模为一个挂起的函数并在ViewModel中发出数据时,我在应用程序中遇到了一个崩溃,没有堆栈跟踪问题:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.x.x.x.debug, PID: 14247
导致崩溃的定义:
服务
interface Service {
@GET("v1/count")
suspend fun getCounts(): Response<Counts>
}
存储库:
suspend fun getCounts(): Response<Counts> {
return withContext(Dispatchers.IO) { service.getCounts() }
}
ViewModel
fun getCounts(): LiveData<Response<Counts>> = liveData { emit(repository.getCounts()) }
片段中的观察者:
viewModel.getCounts().observe(viewLifecycleOwner, {
if (it.isSuccessful) {
...
}
})
定义工作正常:
interface Service {
@GET("v1/count")
fun getCounts(): Call<FeesCounts>
}
注意:如果Interceptor中没有抛出异常,则上述两种情况都不会发生崩溃。
你有没有想过这种行为的根本原因是什么?
谢谢。
这样修改:
data class Hello (
val count: Long = 0
)
保留";计数";与JSON响应相同。