gpt4 book ai didi

firebase - 在哪些情况下我可以获得带有尚未设置的 Firestore serverTimestamp 的 docSnapshot.data()?

转载 作者:行者123 更新时间:2023-12-04 15:17:08 27 4
gpt4 key购买 nike

刚好遇到这 block firebase docfirebase.firestore.SnapshotOptions 上:

serverTimestamps

serverTimestamps: "estimate" | "previous" | "none"If set, controls the return value for server timestamps that have not yet been set to their final value.

By specifying 'estimate', pending server timestamps return an estimate based on the local clock. This estimate will differ from the final value and cause these values to change once the server result becomes available.

By specifying 'previous', pending timestamps will be ignored and return their previous value instead.

If omitted or set to 'none', null will be returned by default until the server value becomes available.

这让我很担心,因为我正在尝试将客户端生成的日期转换为服务器时间戳。我认为,每当我读取使用 serverTimestamp() 保存的文档快照时,我都可以 100% 确定在该点设置日期。但从上面的文档来看,我似乎无法做出这样的假设。

作为一般性问题:

在什么情况下我可以找到尚未设置的serverTimestamp?是否存在可能成为问题的常见用例?

具体我想了解一下这两种情况:

案例 1

  • 使用字段 createdAt: firebase.firestore.FieldValue.serveTimestamp()
  • 将文档保存到 firestore
  • 我有一个带 firestore 的云函数 onWrite | onUpdate 触发该特定集合。

当该函数运行时,是否有可能 createdAt: serverTimestamp 尚未设置?

案例 2

  • 使用字段 createdAt: firebase.firestore.FieldValue.serveTimestamp()
  • 将文档保存到 firestore
  • 在客户端上,我有一个用于我刚刚保存/更新的doc 的监听器。

当该监听器使用新的 docSnapshot 数据触发时,是否有可能 createdAt: serverTimestamp 尚未设置?


更新

刚找到这篇文章The secrets of Firestore’s FieldValue.serverTimestamp()

似乎对于CASE 2,这只会发生在本地写入中。鉴于这一事实,是否可以安全地假设在案例 1 上,由于它不是本地写入,它永远不会发生在正在监听 firestore 触发器的云功能上?

Firebase docs on Local Changes

Local writes in your app will invoke snapshot listeners immediately. This is because of an important feature called "latency compensation." When you perform a write, your listeners will be notified with the new data before the data is sent to the backend.

Retrieved documents have a metadata.hasPendingWrites property that indicates whether the document has local changes that haven't been written to the backend yet. You can use this property to determine the source of events received by your snapshot listener.

最佳答案

When that listener fires with the new docSnapshot data, is there a possibility that the createdAt: serverTimestamp has not been set yet?

这正是您所期望的。由于服务器时间戳只能在服务器上设置,因此您必须等待来自服务器的快照。您在写入时从事件监听器获得的第一个快照将不包含最终时间戳值。

关于firebase - 在哪些情况下我可以获得带有尚未设置的 Firestore serverTimestamp 的 docSnapshot.data()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64134437/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com