如何在Android App Architecture中组合两个数据流?
我根据 app Architecture指南。我已经将用户的数据存储在 ROOM数据库
与 firebase cloud Firestore
和用户的active-status同步,并在 firebase firebase实时数据库
中。
在 dataSource
层中,用户的数据以 paggingdataflow
的形式公开,单个用户的活动状态以 flow flow
的形式公开。来自活动
/ fragment
层,这两个组合在一起形成一个UI状态。现在,在存储库
或 viewModel
中,我必须组合这两个并形成 userUi
单位,然后将该流程传递给>
查看
层。我想我必须在存储库
图层中执行此操作。但是我不知道该怎么做。
----------------- ------------------
| UserLocalDS | | UserRemoteDS |
----------------- ------------------
↑ | ↑ |
(no_arg)| |Flow<PagingData<User>> user_id| |Flow<UserStatus>
| ↓ | ↓
----------------------------------------------------------------
| UserRepository |
----------------------------------------------------------------
|
|Flow<UserUI>
↓
--------------
| UserListVM |
--------------
|
|Flow<UserUI>
↓
--------------
|UserListView|
--------------
userLocalds
的每个发射都是 pagingdata&lt; user&gt;
,其中包含 user
的分页列表。对于每个这样的 user
,我必须将 user.user_id
传递给 userremoteds
并获得 flow&lt&userstatus&gt;
,,发出 userstatus
,必须将其与 user
合并,以导致 pagingdata&lt; userUi&gt;
。
这是预期的伪代码。但这是不合适的。
pagedUsersFlow = userLocalDS.getPagedUsersFlow()
pagedUsersFlow.foreach { user ->
userStatusFlows[user.user_id] = userRemoteDS.getUserStatusFlow(user.user_id)
}
pagedUsersFlow.transform { user ->
user + userStatusFlows[user.user_id].collect()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
怎么样:
样本输出:
这将产生每个用户的用户更新的流程。每种排放都将包含用户和状态。
要简化事物,请以
pair&lt; int,字符串&gt;
作为用户
和userstatus
的组合flow&lt; list&lt; int&gt;&gt;
作为flow&lt; pagingData&lt; user&gt;&gt;&gt;
,以及flow flow&lt; string&gt; as
as
as
flow&lt&lt&lt; lt; userstatus&gt;
。您可能不应该使用
流。*最新的
运算符,因为它们会在有后压时丢弃排放。如果调用getUserStatusflow
对于整个pagingdata&lt; user&gt;
都比flow flow&pagingdata&lt;用户&gt; gt;
正在排放pagingData&lt;用户&gt;
。您需要一种将
pagingData&gt;
转换为flow&lt; user&gt;
的方法。 (我使用l.asflow()
进行此操作,其中ASFLOW
由kotlinx.coroutines
)提供,如果PIGGDATA
是一种允许迭代的集合。如果您不知道该怎么做,请在评论中ping我,我会添加它。How about this:
Sample output:
This will produce a flow of the user updates of every user. Each emission will contain both the user and the status.
To simplify things, take
Pair<Int, String>
as the combination ofUser
andUserStatus
, i.e.UserUI
,Flow<List<Int>>
asFlow<PagingData<User>>
, andFlow<String>
asFlow<UserStatus>
.You probably shouldn't use the
Flow.*latest
operators because they will drop emissions when there's back-pressure. And there could be back-pressure if callinggetUserStatusFlow
for an entirePagingData<User>
is ever slower thanFlow<PagingData<User>>
is emittingPagingData<User>>
.You'll need a way to convert a
PagingData<User>
to aFlow<User>
. (I do this withl.asFlow()
, whereasFlow
is provided bykotlinx.coroutines
) That should be pretty straightforward ifPagingData
is a type of collection that allows iteration. If you don't know how to do this, ping me in the comments and I'll add it.我想出了以下实现:
Update :
该解决方案的绘图是,如果用户状态在
firestore上更改,则不会更新用户状态。实际上,我认为使用
pagingData
实现的目标没有一个很好的解决方案。我建议重新考虑您的数据结构并使用list
而不是pagingdata
。I've come up with the following implementation:
Update:
The drawaback of this solution is that it will not update user status if it is changed on
Firestore
. Actually I don't think there is a good solution to what you want to achieve usingPagingData
. I would recommend to reconsider your data structures and useList
instead ofPagingData
.