Skip to content

Instantly share code, notes, and snippets.

@Turskyi
Last active July 24, 2021 10:23
Show Gist options
  • Save Turskyi/f428c283f47b7b59104aa8da1a8a4f61 to your computer and use it in GitHub Desktop.
Save Turskyi/f428c283f47b7b59104aa8da1a8a4f61 to your computer and use it in GitHub Desktop.
Convenient extensions for LiveData in Kotlin
fun <T> LiveData<T>.observeOnce(lifecycleOwner: LifecycleOwner,observer: androidx.lifecycle.Observer<T>) {
observe(lifecycleOwner, object : androidx.lifecycle.Observer<T> {
override fun onChanged(t: T) {
observer.onChanged(t)
removeObserver(this)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment