Jason Ankers
03/24/2021, 5:34 AMval uiState = combine(sharedFlow1, sharedFlow2, sharedFlow2) { … }
and I would like to catch anything which goes wrong in any of the flows. How could I achieve that? .catch
doesnt work because shared flows never completeResult
type which gets emitted from my shared flows, and create a flow extension fun <T> Flow<Result<T>>.resultOrError()
which maps the errors to exceptionswasyl
03/24/2021, 7:04 AMSharedFlow
state
All errors and completion signals should be explicitly materialized if needed
Jason Ankers
03/24/2021, 7:39 AMfun <T, U> Flow<Result<T, U>>.resultOrError(): Flow<T> { result ->
return map { result ->
when(result) {
is Result.Success -> result.body
is Result.Error -> throw Exception(result.error)
}
}
}
val flow1 = flow {
// catch errors and convert to Result.Error
}.shareIn(..).resultOrError()
val flow2 = flow { .. }.shareIn(..).resultOrError()
val combined = flow1.combine(flow2).catch(// handle errors)
wasyl
03/24/2021, 8:03 AMJason Ankers
03/24/2021, 8:05 AMResult
type is it’s not easy to quickly determine if anything failed during a large combine
. I.e. if I’m combining 4 different data sources, I would need to individually check if each source is of type Result.Error
. catch
is a nice shortcut to grab any error