javascript - 网络上的 Firestore 是否对两个听众收取两次读取费用?

标签 javascript firebase google-cloud-platform google-cloud-firestore angularfire

使用 Firestore Web API v9,假设我有一个集合监听器和一个用于该集合中文档的文档监听器。该文档的更新已通过。

两个问题:

  1. 两次阅读是否收费?
  2. 这通常是一种可接受的做法吗? SDK 是将数据发回两次,还是发送一次并分派(dispatch)更新两次?

例子:

collectionSnapshots(collection(firestore, 'people')).subscribe(...);

docSnapshots(doc(firestore, 'people/john')).subscribe(...);

最佳答案

Am I charged for two reads?

无论您拥有多少听众,您都需要根据阅读的文档数量付费。如果“people”集合有 50 个文档并且监听器返回 50 个文档,则您需要支付 50 次阅读费用。以后收到的任何更新每次更新都将收取 1 次阅读费用。

Does the SDK send the data back twice, or send it once and dispatch an update twice?

添加监听器后,它将返回与您的查询匹配的所有文档(在本例中为完整的人员集合)。之后,每当添加/更新/删除文档时,监听器都会收到更新。

是的,如果你有一个关于人物集合的监听器和另一个关于该集合文档的监听器。两个监听器都将获取更新的数据,并且您将为该单个更新支付 2 次读取费用。不过,您不需要仅针对单个文档的另一个监听器。您可以从收到的更改中读取文档 ID,并在您的网络应用中更新它。

import { collection, query, where, onSnapshot } from "firebase/firestore";

const q = query(collection(db, "people"));

const unsubscribe = onSnapshot(q, (snapshot) => {
  snapshot.docChanges().forEach((change) => {
    if (change.type === "modified") {
      // a document in people collection is modified
      const docId = change.doc.id; 
      const docData = change.doc.data();
    }
  });
});

关于javascript - 网络上的 Firestore 是否对两个听众收取两次读取费用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71634119/

相关文章:

node.js - 在 firebase 函数中编写响应操作时出现问题

google-cloud-platform - 云SDK : gcloud sql instances describe throws error 404: The Cloud SQL instance does not exist

javascript - 如何使用 Internet Explorer 中的复选框使用 Javascript 和 CSS 隐藏和取消隐藏

java - 添加从我的 RecycleView 中选择的项目

javascript - promise 与 Q 的序列组合

firebase - 如何在 `screen_view` Firebase事件上建立 channel

java - 如何使用 Objectify 在 Google Cloud Platform 中的多个实体上使用事务?

python - 如何将数据帧导出到谷歌云存储桶中的csv

javascript - 在 JavaScript 中拖放

javascript - .trigger() jquery 函数导致 Uncaught RangeError : Maximum call stack size exceeded