I have 2 live data, I add them as sources to a mediator live data, I expose this from a view model for a fragment to observe.
When either of the live data changes it triggers the onChanged method of the mediator live data, which means my observer gets triggered twice, what I want is to combine the two and observe just one stream that only triggers once, is there something I'm missing or do I need to roll my own method for this? currently I do this, which triggers twice
SOME CODE REMOVED FOR BREVITY
public CardViewModel(@NonNull Application application , int clicks, String[] cardArgs){
sentenceRepository = new SentenceRepository(application);
cards = Transformations.switchMap(search, mySearch -> sentenceRepository.searchLiveCardListByWordTypeAndWordDescriptionAndSearchWord(cardArgs[0],cardArgs[1],mySearch));
groupRepository = new GroupRepository(application);
groups = groupRepository.getGroupsByWordDescriptionAndWordType(cardArgs[0],cardArgs[1]);
sentencesAndGroups = new MediatorLiveData<>();
sentencesAndGroups.addSource(cards, sentences -> {
Log.d(TAG,"addSource cards");
sentencesAndGroups.setValue(combineLatest(sentences, groups.getValue()));
});
sentencesAndGroups.addSource(groups, groupsWithSentences -> {
Log.d(TAG,"addSource groups");
sentencesAndGroups.setValue(combineLatest(cards.getValue(), groupsWithSentences));
});
}
private List<Visitable> combineLatest(List<Sentence> sentenceList, List<GroupsWithSentences> groupsWithSentences) {
List<Visitable> visitableList = new ArrayList<>();
if (sentenceList != null){
visitableList.addAll(sentenceList);
}
if (groupsWithSentences != null){
visitableList.addAll(groupsWithSentences);
}
return visitableList;
}
any help?
You can use a
BiFunction
. In my project I have a class which contains this:You can use it like this:
I do not fully understand what lists you have and what you want to observe. The example I gave here can observe two
LiveData
objects and triggers when one of them needs to update. It will always give you the most up to date version of theVisitable
objects.If I don't understand the problem, can you explain what
LiveData
objects need to be combined and what the result needs to be?In the case, that an extra
LiveData
object needs to be observed in theMultipleDataTransformation
, it is possible to make atriMap
.Edit: You can try this: