arekolek
01/14/2018, 12:07 PMfun <T> LiveData<T>.distinct(): LiveData<T> {
var lastValue: Any? = Any()
return MediatorLiveData<T>().apply {
addSource(this@distinct) {
if (it != lastValue) {
lastValue = it
postValue(it)
}
}
}
}
Or using a property of the object:
fun <T> LiveData<T>.distinct(): LiveData<T> {
return MediatorLiveData<T>().apply {
addSource(this@distinct, object : Observer<T> {
private var lastValue: Any? = Any()
override fun onChanged(value: T?) {
if (value != lastValue) {
lastValue = value
postValue(value)
}
}
})
}
}
Any arguments to prefer one over the other?Czar
01/14/2018, 1:20 PMkevinmost
01/14/2018, 4:33 PM