Is it possible to prevent LiveData receive the last value when start observing?
I am considering to use LiveData as events.
For example eve
According to answer of jurij-pitulja.
if we are using kotlin coroutines the solution look likes this.
class Event(private val content: T) {
var isHandled = false
private set
fun getContentIfNotHandled(): T? {
return takeIf { !isHandled }?.let {
isHandled = true
content
}
}
}
Inside of view model class replacing Flow.asLiveData() into emit new Event
val authResult: LiveData>> = _emailLiveData.switchMap { email ->
liveData{
repository.authRequest(email).collect{
emit(Event(it))
}
}
}
Implementing observer method inside of fragment
viewModel.authResult.observe(viewLifecycleOwner){
it.getContentIfNotHandled()?.run {
onAuthRequestComplete(this)
}
}