https://kotlinlang.org logo
#coroutines
Title
# coroutines
n

Nick Williams

02/14/2021, 2:45 PM
I know Flow<T>.combineLatest is deprecated as it is just called combine but is there a true combineLatest (in the same vain as transformLatest, collectLatest, mapLatest, flatMapLatest) or does combine already behave this way (I don't believe so because the docs don't mention it)
a

Alex Vasilkov

02/14/2021, 3:07 PM
It is not deprecated in a sense that it was ever a thing in Kotlin, it was added just to ease the migration from libs like RxJava. From
Flow.comabine
docs:
Returns a [Flow] whose values are generated with [transform] function by combining the most recently emitted values by each flow.
I think this is what you would expect from this kind of operator. “Latest” in operators like
flatMapLatest
has a bit different meaning, it actually subscribes to latest produced
Flow
and unsubscribes from any previous flows (like RxJava’s
switchMap
).
w

wasyl

02/14/2021, 3:10 PM
(and I deleted my answer because I realised it was wrong)
Anyway if I understand correctly you’re right, if you suspend in
transform
block and new values are emitted from the combined flows, then the transformation is not cancelled. If you want cancellation then I suppose the simplest way would be moving the work you want cancelled to a separate,
*Latest
operator, for example instead of
Copy code
flow1
  .combine(flow2) { a, b -> heavyTransform(a, b) }
you can do
Copy code
flow1
  .combine(flow2) { a, b -> a to b }
  .mapLatest { (a, b) -> heavyTransform(a, b) }
n

Nick Williams

02/14/2021, 3:21 PM
@wasyl yes this is exactly what I was thinking, in fact I think I will create a extension function that chains these two calls together and call it combineLatest... which is back to my original point: feels like this operator should exist in the library already, I suspect the only reason it doesn't is to avoid the confusion for people coming from Rx
28 Views