android - 批量更新会触发多少次 onUpdate 函数?

标签 android google-cloud-firestore google-cloud-functions

我想更新名为 recipes 的集合中文档中的两个字段,我使用了批量更新来完成它:

let recipeRef = db.collection('recipes').doc(`${recipeId}`);
let batch = db.batch();
batch.update(recipeRef, {ratingsCount : admin.firestore.FieldValue.increment(1)});
batch.update(recipeRef, {totalRating : admin.firestore.FieldValue.increment(snap.data().rating)})
return batch.commit();

我在 recipes 集合上有一个触发函数,如下所示:

exports.RecipeUpdated = functions.firestore
.document('recipes/{recipeId}')
.onUpdate((change, context) => 
{
    //code
});

我的问题是因为在上面的批处理中有两个更新,这是否也会触发 onUpdate 函数两次或者因为批处理写入是原子完成的,触发器将只被调用一次?我希望触发器只被调用一次。

最佳答案

正如@DougStevenson 在他的评论中提到的,您应该只运行代码并查看行为。但请注意,即使您使用相同的 recipeRef,您也会在您的批处理中创建两个不同的更新。在这种情况下,您将得到的结果是 onUpdate() 将触发两次。

如果你只想被调用一次,那么你应该创建两个不同的更新,你可以创建一个。 update()函数允许您传递多个可以更新的属性。如果您使用的是对象,而不是简单地获取对文档的引用、获取它、进行更改并将其写回。

关于android - 批量更新会触发多少次 onUpdate 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58481952/

相关文章:

node.js - Firebase Cloud Functions 与 TypeScript : Realtime Database update ends with success but not updates anything, JS 工作正常

firebase - Firebase 的 Cloud Functions 被多次调用

java - 如何在 ViewPager Fragment 中更改 View

android - 在警报管理器中显示通知

android - Android 5.0+系统Toast不显示

android - 图像处理 : "dalvikvm: external allocation too large for this process" error

android - 如何检查某个数据是否已经存在于firestore中

typescript - CollectionReference.doc() 要求它的第一个参数是非空字符串类型

javascript - Firebase Firestore db.collection 不是函数

firebase - 如何从 Cloud Function 调用其他 Cloud Firebase Functions