Is it safe to combine a flow with something derive...
# coroutines
n
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
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
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
I think you want to use
zip
instead of combine here
combine might have timing issues and emit more values than you expect
n
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,
data
and
hasData
could be inconsistent.
t
Also why not just use an onEach instead?