In general is it safe to assume the following are roughly similar in cost?
Copy code
suspend fun getX() : X
fun getXFlow(): Flow<X>
val a = getX()
val b = getXFlow().first()
Obviously the implementations may be different (one off query vs query and subscribe). But can the implementation optimise the call to first() which is the when the subscription happens and short circuit it? Othewise it's tempting to put both variants on most repository methods. But it would be nice if there was an expectation that first() should optimise to the suspend fun case.
a
Adam Powell
12/03/2021, 2:40 PM
No, it's not safe to assume that. In the case of something like a database query it's the difference between performing a one and done query vs. performing that same query and setting up/tearing down table change observers, the cost of which could vary dramatically depending on your data source
👍🏻 1
👍 1
Adam Powell
12/03/2021, 2:41 PM
Instead of taking an all or nothing stance from the repository in isolation, offer the operations your layers above actually use. Many calls will only need one or the other.
y
yschimke
12/03/2021, 6:13 PM
Yep, that's what I figured. I'll end up exposing suspend and flow variants of both in a lot of cases.
yschimke
12/03/2021, 6:14 PM
I'd like to have enough understanding of the typical implementation flow to consider if it should be the case that flow.first() can be optimised to have the same cost/effect as the suspend form. But I think in reality, most implementations will be coded as you suggest.
yschimke
12/05/2021, 10:25 AM
Ok, never fixable since it's an extension method never seen by implementations.