Its been a while since the original post but I recently stumbled upon the same issue (also with Firebase) and I was able to solve it with Transformations.
I have a repository class that holds liveData objects, collected with Firebase's ValueEventListener.
The ViewModel hold a reference to this repository.
Now, in the ViewModel, instead of having a function that returns the LiveData value from the repository, and then pass it to the Fragment via an observer , like this:
fun getMyPayments(): LiveData<HashMap<String, Int>> {
return repository.provideMyPayments()
}
I use a val with Transformations.map that represent the final result of the LiveData, after being processed by another function in the ViewModel:
val myRoomPaymentsList : LiveData<HashMap<String, HashMap<String, Payment>>> = Transformations.map(repository.provideMyPayments()) {data ->
getRoomPaymentsList(data)
}
note that the first parameter is the data source which you observe and the second parameter is the result you want to get.
This val is a LiveData val that holds to the most current value from the repository and serves it as needed in the Fragment, keeping all the processing in the ViewModel and only the UI function inside the Framgent itself.
Then, inside my Fragment, I put an observer on this val:
viewModel.myRoomPaymentsList.observe(viewLifecycleOwner, {
roomPayments = it
graphFilterPeriod()
})