Android MediatorLiveData将多个LiveData结果合并为单个LiveData对象在Java中

问题描述

我正在使用来自独立表的2个LiveData到应用程序的存储库中。我将两个LiveData作为源添加到扩展CustomMediatorLiveData的{​​{1}}类中。 在每个MediatorLiveData的{​​{1}}方法onChanged回调中,我将两个addSource的值发送到组合了两者并返回单个{{1} }设置为LiveData对象的LiveData。我正在我的存储库中创建此LiveData的对象,并将两个value传递为构造函数的参数。

这会运行并且不会出现任何错误,但是会弄乱LiveData本身中的数据。

示例:如果该日期最初是2020年8月15日,则可能是0001年8月14日。

CustomMediatorLiveData:

CustomMediatorLiveData

Note Repository类:

CustomMediatorLiveData

我尝试使用LiveData对象,并将两个public class CustomMediatorLiveData extends MediatorLiveData<List<Object>> { private List<Note> notes = Collections.emptyList(); private List<RecurringTask> recurringTasks = Collections.emptyList(); public CustomMediatorLiveData(LiveData<List<Note>> liveNotes,LiveData<List<RecurringTask>> liveRecurringTasks) { addSource(liveNotes,notes1 -> { if (notes1 != null) { this.notes = notes1; } setValue(combineData(notes,recurringTasks)); }); addSource(liveRecurringTasks,recurringTasks1 -> { if (recurringTasks1 != null) { this.recurringTasks = recurringTasks1; } setValue(combineData(notes,recurringTasks)); }); } // This method adds the 2 lists into one and sorts them based on dates and priority. private List<Object> combineData(List<Note> notes,List<RecurringTask> recurringTasks) { List<Object> combinedList = new ArrayList<>(); if (notes != null && !notes.isEmpty()) combinedList.addAll(notes); if(recurringTasks!=null && !recurringTasks.isEmpty()) combinedList.addAll(recurringTasks); Collections.sort(combinedList,new Comparator<Object>() { @Override public int compare(Object o1,Object o2) { Date d1,d2; Note n1 = null,n2 = null; RecurringTask r1 = null,r2 = null; if (o1 instanceof Note && o2 instanceof Note) { int hmm = Boolean.compare(((Note) o2).isPriority(),((Note) o1).isPriority()); if (hmm != 0) return hmm; } if (o1 instanceof Note) { d1 = ((Note) o1).getEnd_date(); n1 = ((Note) o1); } else { d1 = ((RecurringTask) o1).getEnd_date(); r1 = ((RecurringTask) o1); } if (o2 instanceof Note) { d2 = ((Note) o2).getEnd_date(); n2 = ((Note) o2); } else { d2 = ((RecurringTask) o2).getEnd_date(); r2 = ((RecurringTask) o2); } if (n1 != null) { if (r2 != null) { if (n1.isPriority()) { return -1; } } } if (n2 != null) { if (r1 != null) { if (n2.isPriority()) { return 1; } } } long l1 = d1.getTime() - d2.getTime(); if (l1 > 0) { return 1; } else if (l1 < 0) { return -1; } else { return 0; } } }); return combinedList; } } 添加到存储库本身中,并且仍然存在相同的问题。

实现此目的的正确方法是什么?如何将2个 public class NoteRepository { private String DB_NAME = "db_task"; Context context; private RecurringDao recurringDao; private LiveData<List<RecurringTask>> upcomingRecurringTasks; private LiveData<List<Note>> upcomingTasks; private CustomMediatorLiveData customMediatorLiveData; private NoteDatabase noteDatabase; public NoteRepository(Context context) { noteDatabase = NoteDatabase.getInstance(context); recurringDao = noteDatabase.recurringDao(); upcomingRecurringTasks = recurringDao.getUpcomingRecurringTask(); upcomingTasks = noteDatabase.daoAccess().fetchUpcomingTasks(); this.context = context; customMediatorLiveData = new CustomMediatorLiveData(upcomingTasks,upcomingRecurringTasks); } public LiveData<List<Object>> getCustomMediatorLiveData() { return customMediatorLiveData; } public LiveData<List<RecurringTask>> getUpcomingRecurringTasks() { return upcomingRecurringTasks; } public LiveData<List<Note>> fetchUpcomingTasks() { return NoteDatabase.getInstance(context).daoAccess().fetchUpcomingTasks(); } } 组合成一个可以观察到的MediatorLiveData

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)