I want to make sure I understand correctly when is the onChanged method of a LiveData's Observer called.
Let's say we have an object A which has some primitives types
(some ints, some strings, etc.) and an object B as fields.
I know that onChanged is called when I call setValue. I'm pretty sure that it is also called when A's primitive fields change, or when I reassign the object field to a new instance, and that it is NOT called when I change any of B's fields.
Please correct me if I'm wrong.
Do these rules also apply to a LiveData object fetched from Room?
Are there any other cases where onChanged is called?
The fact is that the
onChangedmethod of theLiveData'sObserveris only called when the containing value of theLiveDatais being set usingsetValue()orpostValue()method. There is no mechanism to observe fields inside an object that is held by aLiveData. As a result, by changing the value of fields insideobject-A, the observer shouldn't be notified.On the other hand, as you know, the methods that are provided by Room to query on a db are able to return
LiveData<SomeType>instead ofSomeType. Under the hood, Room creates and registers aContentObserveron your query's table(s) to get aware of changes in it. So, every time a change occurs on the data, Room gets notified using the mentionedContentObserver, then fetches the query result again and post it on theLiveData.