Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 批量更新时onUpdate函数将触发多少次?_Android_Google Cloud Firestore_Google Cloud Functions - Fatal编程技术网

Android 批量更新时onUpdate函数将触发多少次?

Android 批量更新时onUpdate函数将触发多少次?,android,google-cloud-firestore,google-cloud-functions,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 : ad

我想在名为
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();
我在
配方
集合上有一个触发函数,如下所示:

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

我的问题是,由于上述批处理中有两个更新,这是否也会触发
onUpdate
函数两次,或者由于批处理写入以原子方式完成,因此触发器将仅被称为一次?我希望触发器只被调用一次

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


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

如果您只运行代码,这似乎是很容易观察到的。我这样做了,
onUpdate
只调用了一次。可能是一个小问题(或者我错过了什么):为什么您需要使用两个更新,而不是只使用一个<代码>批处理.update(recipeRef,{RatingScont:admin.firestore.FieldValue.increment(1),totalRating:admin.firestore.FieldValue.increment(snap.data().rating)})@RenaudTarnec oh。。我不知道我能做到……事实上这是亚历克斯在回答中解释的。抱歉@Alexmamo在写我的评论之前,我没有仔细阅读你的回答…;-)非常感谢你的回答。我不知道一个查询可以更新多个字段。另外,我在发布这个问题后运行了这个代码,它只调用了一次,尽管你提到它将被调用两次。我不知道这会发生。这就是我等待别人回答的原因。是的,你可以一次更新一个文档中的多个属性。仅看到问题中的代码,我无法告诉您仅调用一次的原因,但可以肯定的是,当您执行两个单独的更新时,即使是批量执行,
onUpdate()
函数将触发两次。