idd*_*lip 3 android android-paging-3
我的PagingSource加载一些数据。文档建议捕获这样的异常,以便将来进行某些处理LoadResult.Error 。
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Item> {
return try {
...
throw SomeCatchableException()
...
} catch (e: SomeCatchableException) {
LoadResult.Error(e)
} catch (e: AnotherCatchableException) {
LoadResult.Error(e)
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这样处理时:
(adapter as PagingDataAdapter).loadStateFlow.collectLatest { loadState ->
when (loadState.refresh) {
is LoadState.Loading -> {
// *do something in UI*
}
is LoadState.Error -> {
// *here i wanna do something different actions, whichever exception type*
}
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道哪个 Exteption 将被捕获,因为我在参数LoadResult.Error(e)中传递了它(Throwable)。
我如何知道loadState处理中的异常类型?
小智 8
在 LoadState.Error 的情况下,您可以从 loadState.refresh 捕获错误,您只是错过了将 loadState.refresh 转换为 LoadState.Error 的操作。或者尝试这样:
when (val currentState = loadState.refresh) {
is LoadState.Loading -> {
...
}
is LoadState.Error -> {
val extractedException = currentState.error // SomeCatchableException
...
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2098 次 |
最近记录: |