Skip to content

Instantly share code, notes, and snippets.

@amay077
Created November 2, 2017 08:52
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 amay077/ca40b915d3d4059a2ce4c41bc75807ce to your computer and use it in GitHub Desktop.
Save amay077/ca40b915d3d4059a2ce4c41bc75807ce to your computer and use it in GitHub Desktop.
LiveData.observe is NOT onChanged.
package your.awesome.package
fun <T> LiveData<T>.observeOnChanged(owner : LifecycleOwner, observer : Observer<T>) : Unit {
var prev : T? = null
this.observe(owner, Observer<T> {
if (!(prev?.equals(it) ?: false)) {
observer.onChanged(it)
}
prev = it
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment