【发布时间】:2021-02-01 23:12:54
【问题描述】:
我有一个看起来像这样的消费者:
// ItemConsumer.kt
try {
job = itemService
.connect()
.flowOn(Dispatchers.IO)
.catch { e ->
throw e
}
.onEach {
// Update UI for each item collected
}
.launchIn(viewModelScope)
} catch (e : Exception) {
// Handle exception
}
我有一个看起来像这样的制作人:
// ItemService.kt (Producer)
fun connect():Flow<Item> = callbackFlow {
check(1 == 0) // This is expected to throw an IllegalStateException
}
我了解 .catch { } 将处理消费者的 .collect { } 或 .onEach { } 块内出现的任何问题。但是如何抛出 callbackFlow { } 构建器内部发生的异常,以便它可以在消费者的 .catch { } 中捕获?
【问题讨论】:
标签: kotlin kotlin-coroutines kotlin-flow