Is it safe to combine a flow with something derived from itself? Stupid example:
Copy code
val data: Flow<String?> = ....
val hasData: Flow<Boolean> = data.map { it != null }
val combined = data.combine(hasData) { data, hasData ->
require(data == null || hasData)
}
Note that hasData does not suspend, and let's assume flow will be collected on a single thread dispatcher.
e
ephemient
01/19/2022, 4:39 PM
it's "safe" but collects
data
twice, which seems unlikely to be what you want - nothing ensures the two collectors are in sync with each other
n
natario1
01/19/2022, 4:47 PM
I see, thanks!
data
is shared so it should be fine. I know it's ugly, I'll change this at some point - for now I'm trying to debug an issue and wanted to rule this out
t
Trevor Stone
01/19/2022, 5:39 PM
I think you want to use
zip
instead of combine here
Trevor Stone
01/19/2022, 5:40 PM
combine might have timing issues and emit more values than you expect
n
Nick Allen
01/19/2022, 5:40 PM
It can lead to bugs. A
Flow
is sequential. Multiple flows, however, are not and can be collected concurrently and there are no guarantees that they will be collected at a similar pace. In your example,