我有一個 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()
,否則之前存在的代碼行是無用的。
添加回答
舉報
0/150
提交
取消