Is it possible to make one LiveData of two LiveDatas?

前端 未结 3 1927
醉梦人生
醉梦人生 2020-12-03 18:59

I have two DAOs, two Repositories and two POJOs. There is some way to create one Livedata of two? I need it to make single list for Recyclerview. POJOs are similar objects.<

3条回答
  •  不思量自难忘°
    2020-12-03 19:23

    Let's say you want to merge LiveData> & LiveData> then, you'll need to take MediatorLiveData (why? because, both of your live data are of different types).

    So, use code like below :

    LiveData liveData1 = getAllExpensesByDay();
    LiveData liveData2 = getAllIncomesByDay();
    MediatorLiveData liveDataMerger = new MediatorLiveData();
    liveDataMerger.addSource(liveData1, value -> liveDataMerger.setValue(value));
    liveDataMerger.addSource(liveData2, value -> liveDataMerger.setValue(value));
    
    
    

    If you need to use specific data from MediatorLiveData then use Transformations on that.

    Check out more here

    提交回复
    热议问题