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

Arjan van Wieringen

06/27/2022, 2:16 PM
Is there an idiomatic way to wait until a job is active? When I collect a hot flow it can be the case that between the ‘launchIn’ of the collect and a new emission to the hot flow the collector job hasn't started. Assume I have no ownership over the replay :)
j

Joffrey

06/27/2022, 2:18 PM
Do you have this problem in real life or in tests? If the latter, you should use utilities like `kotlinx-coroutines-test`'s
runCurrent
or
advanceUntilIdle
to ensure your coroutines have progressed enough
a

Arjan van Wieringen

06/27/2022, 2:25 PM
In real life. In tests I have it working with runCurrent indeed.
j

Joffrey

06/27/2022, 2:28 PM
Ok got it. In any case, using
launchIn
, the job is likely active from the start, but that doesn't mean it reached the suspension on the
collect
- so it would be of no use to you to wait for this event even if you could
Maybe you could do
launch(start = CoroutineStart.UNDISPATCHED) { flow.collect { ... } }
instead of using
launchIn
. That would ensure the first suspension point is reached
a

Arjan van Wieringen

06/27/2022, 2:30 PM
I'll try that, thanks!
You've been ridiculously helpful to me in the last days. Thanks!
🤗 1
j

Joffrey

06/27/2022, 2:33 PM
Glad to hear! I'm happy to help 🙂 Btw sorry I didn't reply on your last post afterwards, I was on mobile and I couldn't really write any code or test anything
a

Arjan van Wieringen

06/27/2022, 2:38 PM
Haha no worries. You've been incredibly helpful.
n

Nick Allen

06/27/2022, 4:04 PM
onSubscription
does not help here. It waits for the subscription on the
SharedFlow
to be setup so if an event is emitted, it won't be missed but it does not wait for the suspension point on
collect
so the “shared" coroutine may not have done anything.
7 Views