为了账号安全,请及时绑定邮箱和手机立即绑定

为什么在调用 WriteResult 之前,batch.commit() 不起作用?

为什么在调用 WriteResult 之前,batch.commit() 不起作用?

繁星淼淼 2023-08-16 10:20:29
我有一个 Firestore 数据库,我在其中写入数据WriteBatch()。因此,我在提交更改时遇到了奇怪的行为。仅当我对变量执行某些操作时,我的数据才会更新或设置batch.commit() 代码应该更好地解释我的问题:......Firestore db = FirestoreClient.getFirestore();       Map<String, Foo> test = new HashMap<>();test.put("Entry1", new Foo("Bar1", 5));test.put("Entry2", new Foo("Bar2", 7));WriteBatch batch = db.batch();DocumentReference ref;for (String key : test.keySet()) {    ref = db.collection("foo").document(key);    batch.set(ref, test.get(key), SetOptions.mergeFields("var1", "var2"));}// if I just call batch.commit() here data will not be overwritten on a change of i.e "Entry1"// but if I call the next 2 lines everything is working as intendedApiFuture<List<WriteResult>> result = batch.commit();result.get();我在代码中添加这些行没有问题,但我试图理解为什么会发生这种情况。
查看完整描述

1 回答

?
倚天杖

TA贡献1828条经验 获得超3个赞

根据您的评论:

我的示例中的最后两行代码。如果丢失,更改将不会写入 firestore。据我了解,是否保存batch.commit()的返回值应该是无关紧要的。

如果缺少这两行代码,更准确地说是带有 的代码commit(),则不会发生任何事情,因为您没有向批处理提交任何内容。是的,你是对的,无论你是否将结果保存batch.commit()result对象中,commit()方法都将始终在该对象上被调用。因此,只有当您调用批处理对象时,才会将批处理写入Firestore中commit(),否则之前存在的代码行是无用的。


查看完整回答
反对 回复 2023-08-16
  • 1 回答
  • 0 关注
  • 69 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信