Firestore:当我在监听快照时添加文档,读取操作会被计费吗?

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

Firestore: Do I get charged for a read operation when adding a document while listening to snapshots?

问题

  1. 监听器是否立即触发,以便UI可以立即更新,而没有用户等待延迟?

  2. 我是否会被收取读取操作费用,因为更新的文档已经存在,无需从Firestore获取?

英文:

I develop an app in Flutter and use Cloud Firestore as Database. To get data in realtime I use following code:

class Database {
  final usersCol = FirebaseFirestore.instance.collection('users');

  Stream<List<DataModel>> getDataModelStream() {
    return usersCol
        .doc('uid')
        .collection('data')
        .orderBy('date', descending: true)
        .limit(30)
        .snapshots()
        .map((list) {
      return list.docs.map((doc) => DataModel.fromFirestore(doc.data()['dataModel'])).toList();
    });
  }
}

To add / update a document in Firestore I use following code:

 Future addDataModel(String date, DataModel dataModel) async {
    await usersCol
        .doc('uid')
        .collection('workouts')
        .doc(date)
        .set({
      'date': date,
      'dataModel': DataModel.toFirestore(dataModel),
    });
  }

Now I add / update a document in Firestore with addDataModel while I am listening to a Stream with getDataModelStream.

I have two Questions:

  1. Does the listener gets triggered immediately so that the UI can get updated without any delay for user?

  2. Do I get charged for a read operation or not since the updated document is already present and don't need to get fetched from Firestore?

答案1

得分: 1

是的,在你的情况下,你会支付一次文档读取费用。尽管服务器读取的信息可能与客户端已经拥有的信息相同,但服务器仍然需要进行文档读取,这是一项收费操作。

针对你更具体的问题:

  1. 在写操作发送到服务器之前,你的监听器会立即触发本地数据,这会产生文档读取费用。

  2. 因为你有一个监听器,当写操作完成时,客户端也会从服务器获取数据。这产生一次文档读取费用。如果文档与第1步中提到的数据不同,你的监听器会再次被调用以获取更新后的数据。

英文:

Yes, in your scenario you pay a document read. Despite the information (likely) being the same as what the client already has, the server has to read the document and that is a charged operation.

To answer your more specific question:

  1. Your listener indeed immediately gets triggered with the local data for the write operation, before it's even sent to the server. This is not a charged document read.

  2. Because you have a listener, the client also gets the data back from the server when the write completes. This is a charged document read. If the document is different then what was raised in step 1, your listener gets called again with the updated data.

huangapple
  • 本文由 发表于 2023年5月14日 02:17:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76244264.html
匿名

发表评论

匿名网友

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

确定