为什么Firestore中的`arrayUnion`发送所有值?

huangapple go评论66阅读模式
英文:

Why does `arrayUnion` in Firestore sends all values?

问题

我希望我能够通过优化应用程序的不同部分来降低成本。

因此,我尝试在所有适当的地方使用 arrayUnion。问题是当我打开网络控制台来监视更改时,我意识到它并没有改变任何东西。事实上,arrayUnion 似乎仍然会将所有字段值发送到网络。

所以我的问题是,如果背后的结果是相同的,那么在应用程序中使用 arrayUnion 的真正用途是什么?

英文:

I was hoping I could reduce the cost of my application trying to optimize different parts of it.

So I tried to use arrayUnion in all places that see fit.
Problem is when I opened the network console to monitor the changes I realized it didn't change anything. In fact arrayUnion seems to be sending all the field values over the network anyways.

So my question is what's the real use of using arrayUnion in one's app if the result in the background is the same?

答案1

得分: 1

当我打开网络控制台监视更改时,我意识到它并没有改变任何东西。实际上,arrayUnion 似乎仍然会将所有字段值发送到网络。

这是因为 Firestore 更新作为完整文档通过网络发送。无论您是否使用 arrayUnion(),都会出现这种行为。但是,服务器端应用的实际更新是原子操作(事务),确保仅向数组添加唯一元素。

如果背后的结果相同,那么在应用中使用 arrayUnion 有什么真正的用途?

使用 arrayUnion() 的好处在于它提供原子操作,这意味着更新作为单个不可分割的操作应用。这确保了对同一文档的多个并发更新不会导致冲突的更改。语法只是优雅执行此操作的一种方式。

甚至可以在 Update elements in an array 代码片段的注释中看到类似的示例,例如:

// 原子性地将新区域添加到 "regions" 数组字段。 ⇐ 👈
await updateDoc(washingtonRef, {
    regions: arrayUnion("greater_virginia")
});

如果不使用 arrayUnion(),那么您需要获取文档,在本地修改数组,然后使用修改后的数组更新文档。这可能会导致竞争条件和不一致的数据,如果多个客户端同时更新数组。

还有一篇由 QuintoAndar Tech Blog Lucca La Fonte Albuquerque Carvalho 撰写的文章,标题是:Firestore 中的竞争条件:如何解决?

英文:

> when I opened the network console to monitor the changes I realized it didn't change anything. In fact arrayUnion seems to be sending all the field values over the network anyways.

This is because Firestore updates are sent over the network as complete documents. This is the indented behavior whether you use arrayUnion() or not. However, the actual update applied on the server side is an atomic operation(Transaction) that ensures only unique elements are added to the array.

> What's the real use of using arrayUnion in one's app if the result in the background is the same?

The benefit of using arrayUnion() is that it provides an atomic operation, meaning that the update is applied as a single, indivisible operation. This ensures that multiple concurrent updates to the same document do not result in conflicting changes. And the syntax is just an ease of doing that thing elegantly.

You can even see in the comments of the Update elements in an array code snippet like

// Atomically add a new region to the "regions" array field. ⇐ 👈
await updateDoc(washingtonRef, {
    regions: arrayUnion("greater_virginia")
});

If we don’t use arrayUnion(), then you would need to fetch the document, modify the array locally, and then update the document with the modified array. This could lead to race conditions and inconsistent data if multiple clients are updating the array simultaneously.

There is also a article written specifying it by QuintoAndar Tech Blog
Lucca La Fonte Albuquerque Carvalho
by the name : Race Conditions in Firestore: How to Solve it ?

huangapple
  • 本文由 发表于 2023年6月12日 16:16:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76454724.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定