gpt4 book ai didi

android - 云防火墙 : does updating the same document twice in a batch count as a single write?

转载 作者:行者123 更新时间:2023-12-04 10:19:13 24 4
gpt4 key购买 nike

我想更新一个字段,在某些情况下,批量更新同一文档的另一个字段:

db.runBatch { batch ->
val myRef = db.document("path/to/document")
batch.update(myRef, "foo", "bar")

if(someCondition) {
batch.update(myRef, "baz", "bar")
}
}

如果 someCondition,这是单次写入还是两次写入?是真的?

作为替代,我可以做这样的事情:
db.runBatch { batch ->
val myRef = db.document("path/to/document")

if(someCondition) {
batch.update(myRef, "foo", "bar", "baz", "bar")
} else {
batch.update(myRef, "foo", "bar")
}
}

但这会导致重复代码并且在更新更多字段的情况下变得非常困惑

最佳答案

它应该只有一次写入,但是如果您对此没有信心,这很容易解决。只需采用不同的方法,您只需构建一次更新 map ,然后将其传递给 update()这需要一个 map :

db.runBatch { batch ->
val myRef = db.document("path/to/document")
val obj = if (someCondition) {
mapOf("foo" to "bar", "baz" to "bar")
}
else {
mapOf("foo" to "bar")
}

batch.update(myRef, obj)
}

关于android - 云防火墙 : does updating the same document twice in a batch count as a single write?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60945044/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com