【问题标题】:There is a delay on updating a Timestamp field compared to any other field与任何其他字段相比,更新时间戳字段存在延迟
【发布时间】:2021-09-19 02:46:52
【问题描述】:

我正在使用 StreamBuilder 流式传输文档,每次文档发生更改时它都会打印 HELLO

StreamBuilder(
            stream: FirebaseFirestore.instance.collection('collection').doc('ABC').snapshots();,
            builder: (_context, snapshot) {
              if (!snapshot.hasData) return CircularProgressIndicator();
              print('HELLO');
              return Container();
            }

现在当我更新文档时

onTap: () => 
        FirebaseFirestore.instance.collection('collection').doc('ABC').update({'fruit':'apple','color':'golden'}),

单词 HELLO 被打印一次,这应该是......但是当我更新时间戳字段和常规字段时

onTap: () => 
    FirebaseFirestore.instance.collection('collection').doc('ABC').update({'fruit':'apple','time': FieldValue.serverTimestamp()}),

HELLO 这个词在控制台中被打印 两次 ......这不应该......

【问题讨论】:

    标签: firebase flutter google-cloud-firestore


    【解决方案1】:

    这实际上是预期的行为。

    以下是将 Firestore 文档字段设置为 serverTimestamp FieldValue 的步骤。

    1. 监听器第一次使用初始内容触发 文档。 (如果文档不存在,快照将 表示它是空的。)这是预期的;这里没有问题。

    2. 然后,当调用 set() 时,侦听器会在写入之前立即触发 击中服务器。 SDK 不会将令牌值传递给 听众。相反,它只是提供 null (目前),因为 最终值未知(目前)。

    3. 然后,在文档获取之后 与服务器同步,监听器再次触发新的 由 Firestore 确定的时间戳值。

    来源:https://medium.com/firebase-developers/the-secrets-of-firestore-fieldvalue-servertimestamp-revealed-29dd7a38a82b

    所以“HELLO”被打印两次的原因是先返回快照数据,在服务器时间戳为空的情况下更新字段后,然后在更新服务器时间戳后返回快照数据。

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 2013-03-27
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-11
      相关资源
      最近更新 更多