Android会议室:处理与LiveData的关系

时间:2017-12-20 14:18:00

标签: android android-room android-livedata

所以我有两个模型(表格),WordGroupsWords,它们之间的关系是一对多。 所以我的模型和Dao看起来像:

@Entity
public class WordGroup {

    @PrimaryKey
    private int id;

    @Ignore
    private List<Word> words;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public List<Word> getWords() {
        return words;
    }

    public void setWords(List<Word> words) {
        this.words = words;
    }
}

@Entity
public class Word {

    @PrimaryKey
    private int id;

    @ForeignKey(entity = WordGroup.class, parentColumns = "id", childColumns = "wordGroupId")
    private int wordGroupId;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getWordGroupId() {
        return wordGroupId;
    }

    public void setWordGroupId(int wordGroupId) {
        this.wordGroupId = wordGroupId;
    }
}

@Dao
public interface Dao {

    @Query("select * from WordGroup where id = :id")
    LiveData<WordGroup> selectWordGroupById(int id);

    @Query("select * from WordGroup")
    LiveData<List<WordGroup>> selectAllWordGroups();

    @Query("select * from Word where wordGroupId = :wordGroupId")
    LiveData<List<Word>> selectWordsByWordGroupId(int wordGroupId);
}

现在我有一个方法可以选择WordGroup并使用Word设置Transformations

public LiveData<WordGroup> getWordGroupWithWords(int id) {
    LiveData<WordGroup> wordGroupLiveData = dao.selectWordGroupById(id);

    LiveData<WordGroup> wordGroupWithWordsLiveData = Transformations.switchMap(wordGroupLiveData, inputWordGroup -> {
        LiveData<List<Word>> wordsLiveData = dao.selectWordsByWordGroupId(inputWordGroup.getId());

        LiveData<WordGroup> outputWordGroupsLiveData = Transformations.map(wordsLiveData, inputWords -> {
            inputWordGroup.setWords(inputWords);
            return inputWordGroup;
        });

        return outputWordGroupsLiveData;
    });

    return wordGroupWithWordsLiveData;
}

现在我想写一个方法来选择所有WordGroup,它们也会为每个Word设置WordGroup个。方法的签名应该是这样的:

public LiveData<List<WordGroup>> getAllWordGroupsWithWords();

1 个答案:

答案 0 :(得分:0)

嗯我尝试了一点,在MediatorLiveData的帮助下我想出了这个解决方案:

public LiveData<List<WordGroup>> getAllWordGroupsWithWords() {
    LiveData<List<WordGroup>> wordGroupsLiveData = dao.selectAllWordGroups();
    LiveData<List<WordGroup>> outputWordGroups = Transformations.switchMap(wordGroupsLiveData, inputWordGroups -> {
        MediatorLiveData<List<WordGroup>> wordGroupsMediatorLiveData = new MediatorLiveData<>();
        for (WordGroup wordGroup : inputWordGroups) {
            wordGroupsMediatorLiveData.addSource(dao.selectWordsByWordGroupId(wordGroup.getId()), words -> {
                wordGroup.setWords(words);
                wordGroupsMediatorLiveData.postValue(inputWordGroups);
            });
        }

        return wordGroupsMediatorLiveData;
    });

    return outputWordGroups;
}

现在它正是我想要的方式,但是存在一些大问题。

  • 首先,如果您使用setValue上的方法MediatorLiveData,它会多次调用观察者,我可以使用postValue

  • 第二,即使您只更改了一个单词,它也会再次为每个wordGroup调用观察者!

当然,它不是最优化的解决方案,但确实有效。我非常乐于接受建议和改进。