Created
March 18, 2020 13:06
-
-
Save NikolaDespotoski/db6dfff90d0ff7023dbe73a0bcb627e9 to your computer and use it in GitHub Desktop.
Lazy Live Data block initializer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun <T> lazyLiveData(function: suspend LiveDataScope<T>.() -> Unit) = object : kotlin.Lazy<LiveData<T>> { | |
lateinit var liveDataCached: LiveData<T> | |
override val value: LiveData<T> | |
get() { | |
if (!isInitialized()) { | |
liveDataCached = liveData { function() } | |
} | |
return liveDataCached | |
} | |
override fun isInitialized(): Boolean = ::liveDataCached.isInitialized | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment