英文:
Firestore: Do I get charged for a read operation when adding a document while listening to snapshots?
问题
-
监听器是否立即触发,以便UI可以立即更新,而没有用户等待延迟?
-
我是否会被收取读取操作费用,因为更新的文档已经存在,无需从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:
-
Does the listener gets triggered immediately so that the UI can get updated without any delay for user?
-
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步中提到的数据不同,你的监听器会再次被调用以获取更新后的数据。
英文:
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:
-
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.
-
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论