Skip to content

Instantly share code, notes, and snippets.

@NikolaDespotoski
Created March 18, 2020 13:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikolaDespotoski/db6dfff90d0ff7023dbe73a0bcb627e9 to your computer and use it in GitHub Desktop.
Save NikolaDespotoski/db6dfff90d0ff7023dbe73a0bcb627e9 to your computer and use it in GitHub Desktop.
Lazy Live Data block initializer
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