Is there an idiomatic way to wait until a job is a...
# coroutines
a
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
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
In real life. In tests I have it working with runCurrent indeed.
j
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
I'll try that, thanks!
You've been ridiculously helpful to me in the last days. Thanks!
🤗 1
j
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
Haha no worries. You've been incredibly helpful.
n
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.