0

how does MutableLivedata work exactly?

UI components observe the MutableLiveData objects, and they are notified of data updates only when there is a change in the observed data. How does MutableLivedata know that there is change in data or new data is added. I know it doesn't get live updates from server about new records, If not from server how exactly are we getting Livedata?

  • If you are using Kotlin, please do not use `LiveData`. [Please use `StateFlow`](https://medium.com/androiddevelopers/a-safer-way-to-collect-flows-from-android-uis-23080b1f8bda). "How does Livedata know that there is change in data or new data is added" -- if you are using `MutableLiveData`, you need to call `setValue()` or `postValue()`. – CommonsWare May 22 '23 at 21:12
  • Yes I'm using Mutablelivedata and in the activity using observables to get data posted from repository or view model using postValue(). As MutableLivedata posts data to observables from there we update UI. How does MutableLivedata know that there is a change in data or new data is added in database. – Sravan Velnati May 22 '23 at 21:22
  • Usually, that is your job as a programmer. You know when the data changes, because you are the one that is changing the data, and so you know when you need to update the `MutableLiveData`. Sometimes, you might be using a framework that helps you here, such as Room for database access. – CommonsWare May 22 '23 at 21:45

0 Answers0