ViewModel documentation
However, ViewModel objects should never observe changes in the observed life cycles of objects, such as LiveData objects.
Another way is that the data implements RxJava and not LiveData, then the advantage is that they will take into account the life cycle.
In the Google example todo-mvvm-live-kotlin, it uses a callback without LiveData in the ViewModel.
, , Activity/Fragment. , callback RxJava ViewModel.
MediatorLiveData ( Transformations) ( ) ViewModel. , MediatorLiveData ( , Transformations), Activity/Fragment. , Activity/Fragment, ViewModel.
fun start(id : Long) : LiveData<User>? {
val liveData = MediatorLiveData<User>()
liveData.addSource(dataSource.getById(id), Observer {
if (it != null) {
}
})
}
viewModel.start(id)?.observe(this, Observer {
})
PS: ViewModels LiveData: Patterns + AntiPatterns, . , , LiveData (, , , Activity/Fragment).