I have a question, I see that there is `collectLat...
# flow
u
I have a question, I see that there is
collectLatest
which will cancel and restart the action when a new value is emitted, and I see there are deprecated
combineLatest
functions, however, they say to replace with
combine
, does that mean that
combine
will cancel the transformation when a new value is emitted from any of the upstream flows? If not, how would I achieve that behavior? I would expect that
combineLatest
would do similar things to
collectLatest
but as I said, those methods are marked as deprecated. Am I missing something in the documentation?
I see that for some circumstances I could use
combine
to produce a tuple of all of the values, and then pair that with
collectLatest
, however, what if part of my
combine
action requires some heavy computation that if a new value comes in, there isn’t any point in completing? Or is one expected to do the above aggregation and then chain into a
transformLatest
?
w
does that mean that
combine
will cancel the transformation when a new value is emitted from any of the upstream flows?
yes no
afaik
collectLatest
name was primarily a helper for migration from RxJava
u
Where is this documented? because it doesn’t seem to be documented on those methods.
I’m trying to validate your answer, and the sample I’ve created (based on the sample in
collectLatest
docs)
Copy code
suspend fun main() {
    val flow1 = flow {
        emit(1)
        delay(50)
        emit(2)
    }
    var count = 0
    val flow2 = flow {
        while(count < 50) {
            emit(count++)
            delay(50)
        }
    }

    val combined = flow1.combine(flow2) { val1, val2 ->
        println("computing $val1 + $val2")
        delay(100)
        println("result ${val1 + val2}")
    }
    combined.collect()
}
seems to indicate that it does not in fact cancel the transformation. (I’m also not seeing anything in the implementation that would cancel it either). Plus running through some issues on github, it would seem that
combine
does in fact not cancel the work every time a new value is emitted from any upstream.
w
🤔 indeed you’re right. For some reason I was sure that
combine
would cancel, especially since the
transform
function is suspendable, otherwise why is that needed?
and https://github.com/Kotlin/kotlinx.coroutines/issues/1484 confirms it. Apologies for the confusion!
s
I think your best bet is to make sure this combining isn’t expensive as you’re saying. I can’t imagine how bunching some variables in a tuple like structure can be so expensive that you would consider it wasted time when new variables come in, what case are you thinking about?
u
Filtering through a list of data based off of user input in Jetpack Compose. In my specific use case, it probably doesn’t make that much of a difference as the data set is relatively small and already on a background thread anyway, but there could be instances where a dataset is large, and since you are going to be getting new results from the new values, you may as well not continue doing work that will produce outdated results, in which case, the proposed alternative of chaining a
combine
with a
transformLatest
is probably what should be happening anyway. That being said, if such usage is frequent, it would add a bit of boilerplate that could be avoided if they were merged, especially if all the
combine
is doing is grouping the values into a tuple only to get destructured in the chained
transformLatest
, which is why a proposal such as
combineTransformLatest
makes sense.