通过新的LiveData< List>更新RecyclerView.从房间动态返回 [英] Updating a RecyclerView by a new LiveData<List> return from Room dynamically

查看:57
本文介绍了通过新的LiveData< List>更新RecyclerView.从房间动态返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个常规的Room-> DAO-> Livedata-> Repositiry-> ViewModel-> RecyclerView应用程序.UI的不同按钮必须将不同的数据列表传递给RecyclerView.
通过按钮单击我想要的:

I have a conventional Room->DAO->Livedata->Repositiry->ViewModel->RecyclerView app. Different buttons of UI must pass different lists of data to RecyclerView.
By button click I want:

  1. 在DAO中进行新的@Query并获取新的LiveData 对象作为回报.
  2. 将此新数据放入RecyclerViewAdapter中,并调用notifyDataSetChanged()以创建新的List外观.

The Dao @Query:

The Dao @Query:

@Query("SELECT * FROM entry_table WHERE path LIKE :path ORDER BY priority DESC")
LiveData<List<Entry>> getNotesOfFolder(String path);    //Returns LiveData with List of Entries

通过Observer的onChanged更新recyclerView,如下所示:

The recyclerView is updated via onChanged of Observer like this:

public class RecyclerViewActivity extends AppCompatActivity {…
Observer<List<Entry>> entryObserver = new Observer<List<Entry>>() {
    @Override
    public void onChanged(List<Entry> entries) {
        recyclerAdapter.setEntries(entries);
    }
};

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.EntryHolder> {…
public void setEntries(List<Entry> entries) {
    this.entries = entries; //setting LiveData content to adapter's List (i.e. entries)
    notifyDataSetChanged();

问题是LiveData从DAO接收新值时,我的观察者未调用onChange方法.我相信是因为此LiveData的内容不是被其他LiveData更改而是替换.

The problem is that my Observer does not call the onChange method when LiveData receives new value from DAO. I believe it is because this LiveData’s content is not CHANGED but REPLACED by another LiveData.

我试图再次为Observer重新订阅LiveData,但它有些起作用,但是当我尝试调用一些常规的Room查询(例如@Delete)时,我遇到了多个(最多10个!)onChange调用,其中一些行为异常并将错误的列表传递给RVadapter.

I tried to re-subscribe the Observer to LiveData again and it somewhat worked, but when I try to call some conventional Room queries like @Delete, I got multiple (up to 10!) onChange calls and some of them behave weirdly and pass the wrong List to RVadapter.

所以有两个问题:

  1. 如何仅调用观察者的onChanged()?
  2. 是否存在将时尚的LiveData对象动态传递给RecyclerView的其他时尚方法?

推荐答案

我试图再次为Observer重新订阅LiveData,但它有些起作用,但是当我尝试调用一些常规的Room查询(例如@Delete)时,我遇到了多个(最多10个!)onChange调用,其中一些行为异常并将错误的列表传递给RVadapter.

I tried to re-subscribe the Observer to LiveData again and it somewhat worked, but when I try to call some conventional Room queries like @Delete, I got multiple (up to 10!) onChange calls and some of them behave weirdly and pass the wrong List to RVadapter.

如果您不首先从 old LiveData对象中退订您的观察者,那将是有道理的...在查询更改时替换您的观察者.

This would make sense if you didn't first unsubscribe your observer from the old LiveData object... the one you replace when your query changes.

如果查询更新,则需要从DAO获取新的LiveData.如果用新的旧LiveData覆盖旧的LiveData,则不仅需要将观察者(重新)订阅到新的LiveData,还需要从旧的Observer取消订阅.否则它将继续存在并不断更新观察者.

If your query updates, you will need to get a new LiveData from the DAO. If you overwrite your old LiveData with the new one, you will not only need to (re-)subscribe your Observer to the new one, you will also need to unsubscribe it from the old one. Otherwise it will live on and keep updating the observer.

这篇关于通过新的LiveData&lt; List&gt;更新RecyclerView.从房间动态返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆