https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

Colton Idle

03/04/2023, 3:47 AM
I have a pretty easy situation (I think) but I'm getting analysis paralysis. appreciate any pointers. I have two network calls. A and B. And my screen is simple. Show return of call A, and return of call B. There is a third item to show on the screen. It's the result of A and B, but next to each other. Again... this is pretty easy BUT the twist is that this last item (AB) should only show once both network calls are done. The easy way I'm thinking of just doing this is having a state variable called aIsDone and bIsDone and then on finish of network call A and network call B I check if both are done, and then show that third item. But this seems kinda crappy, non-idiomatic? ideas?
p

Pablichjenkov

03/04/2023, 4:08 AM
You can use
zip
, is available in all reactive libraries
Although, make sure you set timeouts because if one fails the operator will wait forever. Perhaps another merge type of operator fits better the failure use case
c

Colton Idle

03/04/2023, 4:22 AM
In my case I want to show A and B separately right away but only wait (zip) when showing the last item. For example. If A comes back right away. Show A then wait 5 seconds and B comes back then show B and show AB
p

Pablichjenkov

03/04/2023, 4:40 AM
Ahh I got you better now. I don't have an operator on top of my head. I would do something similar to what you have in mind
😭 1
s

sciack

03/04/2023, 7:59 AM
Not sure but something like:
Copy code
val a = async callA().also(show(it))
val b = async callB().also(show(it)}
show(a,b)
all in a coroutine context?
or similar, don't remember exactly the syntax
e

ephemient

03/04/2023, 9:39 AM
are coroutines even relevant? the way I would handle this is pretty much the same whether it's implemented with coroutines, flows, or anything else
on return, call A causes a change from
state
to
state.copy(a = result)
, and call B causes a change from
state
to
state.copy(b = result)
whatever UI is observing
state.a
,
state.b
, and
state.a && state.b
just works