Nino
03/31/2023, 5:45 PMsuspend
functions, sometime it needs `Flow`s, so most of the time I just code the Flow
implementations of the Repository interfaces (which are consumed by my domain), and just use Flow.first()
to "transform" the flow to a suspending function.
But it feels weird. I guess, performance wise, that the implementation of a suspending function can be more performant than the Flow one (+ first()). Even more if the Flow is constructed via a callbackFlow
or other Channel-backed flows (which seem "heavier").
I have no metrics tho. What are you thoughts ?kevin.cianfarini
03/31/2023, 5:51 PMFlow.first
can potentially just be wrong. Especially in the case where a Flow is amended to include an onStart { emit(default) }
yschimke
03/31/2023, 5:53 PMyschimke
03/31/2023, 5:54 PMyschimke
03/31/2023, 5:55 PMephemient
03/31/2023, 6:02 PMfirst
doesn't introduce any extra suspensionephemient
03/31/2023, 6:04 PMflow.first()
behaves similarly to
lateinit var result
try {
flow.collect {
result = it
throw Exception()
}
} catch (_: Exception) {
return result
}
the real implementation handles more corner cases but that's the basic ideayschimke
03/31/2023, 6:05 PMyschimke
03/31/2023, 6:05 PMyschimke
03/31/2023, 6:06 PMuli
04/01/2023, 11:27 AMSometimes my domain needsDo you mean 1. the same data source is sometimes used reactive and somtimes just read? 2. some sources are single value, some streams? If 2), I’d strongly suggest to make that visible and declare the single value sources (i.e. retrofit calls) as suspend functions. If it is 1., excessive use offunctions, sometime it needs `Flow`s,suspend
first
in a reactive environment could be a red flag that indicates an inconsistent architecture. Why not go fully reactive?
If your use of `first`is rare then don’t worry about optimization.yschimke
04/01/2023, 11:32 AM