2

I want to change a LiveData value based on two others LiveData values but I don't know how to do it.

I know that I can use LiveData.map() but that only works if I want to change a LiveData value based on another LiveData value.

I have a Repository class with this two LiveData variables:

val isLoadingDataFromDatabase = listForecasts.map { it.isEmpty() }
val isLoadingDataFromNetwork = MutableLiveData<Boolean>().apply { value = true }

Then, I have a ViewModel with this LiveData variable:

val showLoadingIndicator = repository.isLoadingDataFromDatabase

I could also use:

val showLoadingIndicator = repository.isLoadingDataFromDatabase.map { it == true }

However, what I would like to do it to change showLoadingIndicator value to true only when isLoadingDataFromDatabase and isLoadingDataFromNetwork are both true.

I can't use the observe() method because it is used inside the ViewModel and it doesn't have a lifecycle

  • 2
    MediatorLiveData is a solution to combine multiple source read this https://stackoverflow.com/a/52306675/7972699. This should help you – Anmol Aug 10 '20 at 08:40

0 Answers0