在Android中使用状态流进行Mutablelist

发布于 2025-01-25 20:28:33 字数 701 浏览 3 评论 0原文

在一个项目中工作,在一个项目中,我们需要在视图模型中列出项目列表。该类型是自定义数据类。

到目前为止,我已经使用了mutableLivedAta存储列表的状态。当用户在我们已连接到设备的RFID读取器上扫描项目时,将更新列表。扫描时,将从服务器中获取一个项目,并将相应地更新列表。

我想尝试尝试使用stateflow,但我在这里遇到了问题。

扫描新项目时,我将使用add命令更新可变列表,然后在列表中更新相应的项目。这不会触发livedatastateflow observers/collectors - 但是使用livedata,我可以在完成更新后将列表分配给自己它。 (_listofitems.value = _listofitems.value),因为这会通知观察者。

但是,这不适用于stateflow,因为它仅在引用更改时触发(由于它是同一列表,因此仅在其中使用了新/更改的项目)。

我知道,使用可变列表和Coroutines更新同一列表可能存在一些问题,因为如果试图同时更新列表,或类似的内容可能会发生碰撞。

因此,通常的问题是:每当列表的内容更改时,在视图模型中使用列表并进行UI更新时,最好的方法是什么?

Working on a project where we need to have a list of items in a view model. The type is a custom data class.

Until now, I have used a MutableLiveData to store the state of the list. The list will be updated when a user scans an item on an RFID reader that we have connected to the device. When scanned, an item will be fetched from a server and the list will be updated accordingly.

I want to try and move on to using a StateFlow instead, but I'm running into an issue here.

When a new item is scanned, I'm updating the mutable list with the add command and later on updating the corresponding item in the list. This does not trigger the LiveData or StateFlow observers/collectors - but with LiveData I could assign the list to itself after I'm done with updating it. (_listOfItems.value = _listOfItems.value) as this would notify observers.

This does not work with StateFlow however, since it only trigger when the reference changes (which it doesn't since it's the same list, just with new/changed items in it).

I'm aware that there are probably some issues with using a mutable list and coroutines to update the same list, since it might collide if trying to update the list at the same time, or something like that.

So, generally the question is: what's the best approach when having a list in a view model and having the UI update whenever the content of the list changes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2025-02-01 20:28:33

在状态流中使用MutableList是不可能的,并且将其进行更新,因为状态流始终与上一个值的比较等等,并且由于上一个值是同一列表的实例,因此它们将始终被视为相等。

您可以通过共享流程进行此工作,而共享流量不会将值与上一个比较。

但是,通常使用共享流量/状态流(甚至livedata)的可变类的类别非常容易出错,因为数据可能会从多个不同的地方读取并写入并写入。很难管理。

一种解决方案是每次将其发送到流程时创建列表的副本:

myMutableStateFlow.value = myMutableList.toList()

如果您想要更易于管理的代码,可能以性能为代价,请不要完全使用可变的列表。使用仅读取列表。您可以将其放入var属性中,也可以直接修改状态流的value

myMutableStateFlow.value += someNewItems
// where myMutableStateFlow's type is a List, not MutableList

It would be impossible to use a MutableList in a StateFlow and make it update because StateFlow always does an equals comparison with the previous value, and since the previous value is an instance of the same list, they will always be considered equal.

You could make this work with SharedFlow, which does not compare values with the previous.

It is however very error prone to use mutable classes with a SharedFlow/StateFlow (or even LiveData) in general because the data could be getting read from and written to from multiple different places. Very hard to manage.

One solution is to create a copy of the list every time you send it to the flow:

myMutableStateFlow.value = myMutableList.toList()

If you want code that's even easier to manage, possibly at the expense of performance, don't use mutable Lists at all. Use read-only Lists. You could put it in a var property, or just modify the value of your StateFlow directly.

myMutableStateFlow.value += someNewItems
// where myMutableStateFlow's type is a List, not MutableList
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文