Before Lifecycle
and LiveData
stuff, Viewmodel could observe changes to its own observables quite easily. It was only a matter of subscribing to Observable*
's changes and react to them. Then one might used a two-way binding to react immediately on user's input.
In Android Studio Canary, it is now allowed to bind to LiveData objects, provided that Binding knows lifecycle of its owner (ViewBinding now has additional setLifecycle
method), by Android Studio Canary information:
You can now use a LiveData object as an observable field in data binding expressions. The ViewDataBinding class now includes a new setLifecycle method that you need to use to use to observe LiveData objects.
However, Viewmodel documentation states clearly:
ViewModel objects can contain LifecycleObservers, such as LiveData objects. However ViewModel objects must never observe changes to lifecycle-aware observables, such as LiveData objects.
(emphasis mine)
So how one can react immediately to LiveData changes if ViewModel cannot subscribe to them?
And, furthermore, why Viewmodel can not observe changes to its own LiveData?