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

Mark

06/07/2019, 10:24 AM
What is the equivalent of SingletonHolder when using coroutines: https://medium.com/@BladeCoder/kotlin-singletons-with-argument-194ef06edd9e
Copy code
open class SingletonHolder<out T: Any, in A>(creator: (A) -> T) {
    private var creator: ((A) -> T)? = creator
    @Volatile private var instance: T? = null

    fun getInstance(arg: A): T {
        val i = instance
        if (i != null) {
            return i
        }

        return synchronized(this) {
            val i2 = instance
            if (i2 != null) {
                i2
            } else {
                val created = creator!!(arg)
                instance = created
                creator = null
                created
            }
        }
    }
}
g

gildor

06/07/2019, 10:33 AM
Maybe you could share some use case
6 Views