RecyclerView Adapter 依赖于两个数据源 [英] RecyclerView Adapter depends on two sources of data

查看:99
本文介绍了RecyclerView Adapter 依赖于两个数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设

  • 我有一个拥有多个帐户的用户.
  • 每个帐户可能有多个他们控制的页面.
  • 页面可由一个或多个帐户控制.
  • 所有这些数据都存储在具有实体 {UserEntity,PageEntity,User_Page_Entity} 的 Room 数据库中,其中 User_Page_Entity 通过仅存储用户的主键 ID 来控制用户与其页面之间的关系.
  • I have a user with multiple accounts.
  • Each account might have multiple pages that they control.
  • pages could be controlled by one or more accounts.
  • all this data is stored in Room Database with entities {UserEntity,PageEntity,User_Page_Entity}, where User_Page_Entity control the relation between users and their pages by storing only their primary key ids.

我的回收站视图应该显示所有页面,其中一些页面由当前登录的用户控制,其中我的回收站视图的一个项目如下所示:

其中红色/绿色圆圈表示该页面是否属于当前登录的用户.

My Recycler View is supposed to show all the pages, where some of this pages are controlled by the current logged in user where an item of my Recycler View look like this :

where the red/green circle indicate whether this page belongs to the current logged in user or not.

我的问题是我的 Recycler View 依赖于两个数据源:

My problem is my Recycler View depends on two sources of data :

  1. LiveData> 显示存储在数据库中的所有页面.
  2. LiveData> 表示当前用户控制了哪些页面.
  1. LiveData<List<PageEntity>> that shows all the pages stored in database.
  2. LiveData<List<User_Page_Entity>> that indicates which of this pages is controlled by the current user.

因为每当数据库发生变化时,Room 都会同时向所有观察者启动一个通知程序,如果我在两个源上初始化了一个观察者,并且每当它们发生变化时,都会在适配器上启动一个 notifyDataSetChanged,这会导致同时调用两个 notifyDataSetChanged 引发异常,即在两个方法调用之一中分离的持有者之一要求再次分离.

Since Room launches a notifier to all of its observers at the same time whenever there's a change in the database, if I initialized an observer on both sources and whenever they changed launch a notifyDataSetChanged on the adapter, it results in two notifyDataSetChanged called at the same time raising an exception that one of the holders that were detached in one of two method calls, is asking to be detached again.

IllegalArgumentException: called detach on an already detached child ViewHolder

相比之下,我不能只对一个观察者调用 notifyDataSetChanged,因为我不知道 Room 会先调用哪个.

In contrast I can't call notifyDataSetChanged on only one observer since I don't know which one would be called first by Room.

现在我不知道该怎么做以及如何在同一个回收者视图中维护这两个数据源之间的关系,经过两天的思考我完全愿意听取任何建议或建议并实施它应用程序尽可能干净编码

Now I don't know what to do and how to maintain the relation between these two data sources in the same recycler view, after two days of thinking I'm totally willing to hear any suggestion or advice and implement it to make the application as clean coded as possible

推荐答案

这个问题的答案在于我的另一个相关问题 及其答案

answer to this question lies in my other related question and its answer

当然,在有人提供另一个好的解决方案之前,我仍然愿意接受建议,但我想发布我为解决问题所做的工作,以防将来有人看到我的问题.

of course until someone offers another good solution, I'm still open to suggestions but I wanted to post what I did to solve my problems in case someone saw my questions in future.

这篇关于RecyclerView Adapter 依赖于两个数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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