``'让'制作数据的副本吗?

发布于 2025-02-07 06:24:44 字数 646 浏览 2 评论 0原文

在我的ViewModel中,我有此livedata列表:

val liveList: LiveData<List<Int>>

在我的活动中,在某个时候,我访问列表中的数据:

viewModel.liveList.value?.let { letList ->
    while (letList.size < 10)
        delay(100)

某些数据被添加到livelist同时。 (上面的摘要在主线程中不运行)。 Letlist会查看对livelist的这些更新吗?因此,Letlist仍然连接到livelist吗?

还是Letlist livelist在时间点上制作的列表的副本LET执行?因此,如果livelist此时的元素恰好少于10个元素,则循环永远不会终止,无论将多少元素添加到livelist ?

In my ViewModel I have this LiveData list:

val liveList: LiveData<List<Int>>

In my Activity, at some point I access the data in the list like this:

viewModel.liveList.value?.let { letList ->
    while (letList.size < 10)
        delay(100)

Some data gets added to liveList concurrently. (Above snippet doesn't run in the main thread). Will letList see these updates made to liveList? Thus, is letList still connected to liveList?

Or is letList a copy of the list in liveList made at the point in time the let is executed? Thus, if liveList happens to have less than 10 elements at this point, the while loop will never terminate, no matter how many elements are added to liveList?

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

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

发布评论

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

评论(2

迟月 2025-02-14 06:24:46

流放列表会看到对生计的这些更新?

是的,它指向同一对象。

因此,列表仍然连接到生计吗?

它不是真正的“连接”。这只是对同一对象的引用。

或者是在时间点执行的列表中的列表的副本?

不,这不是副本。

因此,如果此时生计碰巧的元素少于10个元素,那么无论添加了多少个元素,while循环永远不会终止?

可能是这种情况,因为包含的列表实例本身可能已在包含的Livedata中被替换。如果将列表实例替换为Livedata内部的新列表,并且该列表会随着时间的流逝而更改,则该循环将永远不会看到新尺寸的列表,因为它会粘在原始列表上。

尽管我质疑这是否是手头任务的最佳方法。也许您应该在列表上设置一个观察者,并在已知会更改时做某事,而不是忙于循环。

Will letList see these updates made to liveList?

Yes, it points to the same object.

Thus, is letList still connected to liveList?

It's not really "connected". It's just a reference to the same object.

Or is letList a copy of the list in liveList made at the point in time the let is executed?

No, it is not a copy.

Thus, if liveList happens to have less than 10 elements at this point, the while loop will never terminate, no matter how many elements are added to liveList?

That could be the case, since the contained list instance itself might have been replaced in the containing LiveData. If the List instance is replaced with a new one inside the LiveData, and that list get changed over time, then the loop will never see a newly sized list since it will be stuck on the original.

Though I question if this is the best approach to the task at hand. Maybe you should set up an observer on the list and do something whenever it's known to change, rather than busy-looping on it.

沉鱼一梦 2025-02-14 06:24:46

该代码将其编译到类似的内容(我将使用Kotlin语法,但您会得到这个想法):

val letList = viewModel.liveList.value
if (letList != null) {
    while (letList.size < 10) {
        delay(100)
    }
}

So Letlist是一个唯一的 variable ,分配了当前值viewModel.livelist.value。这就是为什么运行LET在无效上的block是安全的 - 即使可以通过另一个线程将原始var设置为null,也您正在使用自己的私人副本分配给原始值。您知道在未经检查之后不会变得无效 - 实际上,它根本不会改变!对于捕获该值的快照非常有用,


另一面是该变量的值 - 它是对list对象的引用。如果其他内容具有相同的list的引用,如果它添加或删除了某些项目,您也会看到这些更改 - 因为它都是您都在寻找的列表,

这是您需要的东西小心 - 您可能需要创建自己的列表副本,以免发生这种情况。您不一定知道livedata是如何产生列表值 - 它是否每次创建一个新的list,所以它推动的每个列表都是独立的?还是它会突变一个列表,并且基本上每次都会推动相同的对象,同时用作“这是当前列表”和“如果您有早期列表的副本,它只是更新了?

复制列表可以像thelist.tolist()一样简单,但是它将是不同的 list object 包含相同项目对象。如果这些项目对象可以被突变(例如,如果它们具有可以更改的var),那么即使在完全独立的列表中引用该对象,也可以看到该对象。您必须知道您正在使用的是什么,什么可以保证它给您的

That code compiles to something like this (I'm going to use Kotlin syntax but you get the idea):

val letList = viewModel.liveList.value
if (letList != null) {
    while (letList.size < 10) {
        delay(100)
    }
}

so letList is a unique variable which is assigned the current value of viewModel.liveList.value. That's why running a let block on a nullable var is safe - even if the original var could be set to null by another thread, you're working with your own private copy assigned to the original value. You know it's not going to become null after it's been null-checked - in fact it's not going to change at all! Very useful for capturing that snapshot of a value


The other side to this is just what the value of that variable is - it's a reference to a List object. If something else has a reference to that same List, if it adds or removes some items, you'll see those changes too - because it's the same list you're both looking at

That's something you need to be careful about - you might want to create your own copy of that list so that this can't happen. You don't necessarily know how that LiveData is producing list values - is it creating a new List each time, so every list it pushes is independent? Or is it mutating a single list, and basically pushing the same object every time, acting as both "here's the current list" and "if you have a copy of that list from earlier, it just updated"?

Copying a list could be as simple as theList.toList(), but it'll be a different List object containing the same item objects. If those item objects can be mutated (e.g. if they have a var that can be changed) then wherever that object is referenced, even if it's in completely separate lists, that change will be visible. You have to know what you're working with and what guarantees it gives you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文