2

我有 2 个实时数据,我将它们作为源添加到中介实时数据中,我从视图模型中公开它以供片段观察。

当任何一个实时数据发生变化时,它都会触发中介实时数据的 onChanged 方法,这意味着我的观察者被触发了两次,我想要将两者结合起来并观察一个只触发一次的流,有什么我在丢失还是我需要为此推出自己的方法?目前我这样做,这会触发两次

为简洁起见删除了一些代码

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;
}

有什么帮助吗?

4

1 回答 1

0

您可以使用BiFunction. 在我的项目中,我有一个包含以下内容的类:

public class MultipleLiveDataTransformation {
    @MainThread
    public static <X, Y, O> LiveData<O> biMap(LiveData<X> data1, LiveData<Y> data2, final BiFunction<X, Y, O> biFun) {
        final MediatorLiveData<O> result = new MediatorLiveData<>();
        result.addSource(data1, x -> result.setValue(biFun.apply(x, data2.getValue())));
        result.addSource(data2, y -> result.setValue(biFun.apply(data1.getValue(), y)));

        return result;
    }
}

你可以像这样使用它:

public LiveData<Visitable> getVisitables() {
    return MultipleLiveDataTransformation.biMap(groups, sentences, (gro, sen) -> {
        List<Visitable> visitableList = new ArrayList<>();
        if (gro != null) {
            visitableList.addAll(gro);
        }
        if (sen != null) {
            visitableList.addAll(sen);
        }
        return visitableList;
    });
}

我不完全了解您拥有哪些清单以及您想要观察什么。我在这里给出的示例可以观察两个LiveData对象并在其中一个需要更新时触发。它将始终为您提供对象的最新版本Visitable

如果我不明白这个问题,你能解释一下LiveData需要组合哪些对象以及结果需要是什么?

LiveData如果需要在 中观察到额外的对象MultipleDataTransformation,则可以制作triMap.

编辑:你可以试试这个:

public LiveData<Visitable> getVisitables() {
    return MultipleLiveDataTransformation.biMap(groups, sentences, (gro, sen) -> {
        List<Visitable> visitableList = new ArrayList<>();
        if (gro != null && !gro.isEmpty() && sen != null && !sen.isEmpty()) {
            visitableList.addAll(gro);
            visitableList.addAll(sen);
        }
        return visitableList;
    });
}
于 2020-05-26T08:49:57.743 回答