我有一个 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 intended
ApiFuture<List<WriteResult>> result = batch.commit();
result.get();
我在代码中添加这些行没有问题,但我试图理解为什么会发生这种情况。
倚天杖
相关分类