【发布时间】:2021-10-28 11:44:45
【问题描述】:
我创建了以下扩展函数:
fun <T> Flow<T>.handleErrors(showError: Boolean = false, retry: Boolean = false,
navigateBack: Boolean = true): Flow<T> =
catch { throwable ->
var message: String? = null
if (showError) {
when (throwable) {
is HttpException -> {
postEvent(EventType(retry))
}
}
}
然后扩展函数将 throwable 类型发布到 Base Activity,并根据发布的事件类型显示相关对话框。
如果事件是重试,我想重试失败的流程。
例如,如果 HTTP 异常是 400,我想在对话框中选择重试时重试失败的调用。
是否可以将回调添加到失败的 Kotlin 流,并且可以从不同的活动或片段中调用?
【问题讨论】:
标签: android kotlin-coroutines kotlin-android-extensions kotlin-flow