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

Alessandro Diaferia

02/14/2019, 3:09 PM
Is there some sort of
dispatch_once
in the Kotlin Coroutine world?
m

marstran

02/14/2019, 3:14 PM
What do you mean?
a

antonis

02/14/2019, 3:16 PM
dispatch_once is basically used to create singetons, right?
a

Alessandro Diaferia

02/14/2019, 3:17 PM
I guess I’m looking for a compact way of doing
Copy code
val someSetupJob by lazy {
    async {
        // do some complex initialization
    }
}
a

antonis

02/14/2019, 3:17 PM
you can do that with the object keyword
val myLazyValue = async(start = CoroutineStart.LAZY) { ... }
. Use
myLazyValue.await()
when you need it
a

Alessandro Diaferia

02/14/2019, 3:22 PM
That’s great @antonis thank you!
a

antonis

02/14/2019, 3:23 PM
@Alessandro Diaferia I never actually faced this issue, so thank you for asking 🙂
3 Views