Byte 6 – MediatorLiveData

I’m starting to like MVVM more and more. Though I don’t regret using MVP (we’re rocking the architecture), it’s impossible to not appreciate the simplicity of the things that come with MVVM.

Today I discovered MediatorLiveData, where I’ve used it is for validating data, but the use-cases are many!

This is the code block:

What it does:

  • when the SSN of the user gets changed, the isPersonValid function gets called
  • when the name of the user gets changed, the isPersonValid function gets called

We can add multiple data points as sources to the validation and every time the value gets updated, we trigger the validation without worrying about any overhead caused by remembering the need to call the validation function.

More info about MediatorLiveData can be found here.




Leave a Comment

Blog

Recent posts