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

Alessandro Diaferia

02/12/2019, 3:49 PM
can you guys advise what’s an idiomatic way of handling a suspending initialization? Let’s say my class exposes a
search
function and this function depends on a series of initialization steps that the instance has to perform before being able to actually search and return results. How would you handle this using coroutines? Would you have an
initialize
function that returns a
Job
or would you simply mark
search
as
suspend
and “hide” the suspension as an implementation detail waiting internally for the initialization job to complete? Or would you tackle this issue in a completely different way?
w

withoutclass

02/12/2019, 3:54 PM
I’d probably have the initialization steps return
Deferred
and then if
search
is called, make sure your search function
await
on those steps
👍 3
d

dewildte

02/12/2019, 4:02 PM
Or you could wrap the work in an
actor
and pass the actor the return
Channel
to send the results too.
d

Dico

02/12/2019, 8:38 PM
Would probably use one property to hold a
Job
instance and join it
5 Views