我有一個 Firestore 數(shù)據(jù)庫,我在其中寫入數(shù)據(jù)WriteBatch()。因此,我在提交更改時遇到了奇怪的行為。僅當我對變量執(zhí)行某些操作時,我的數(shù)據(jù)才會更新或設(shè)置batch.commit() 代碼應(yīng)該更好地解釋我的問題:......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();我在代碼中添加這些行沒有問題,但我試圖理解為什么會發(fā)生這種情況。
1 回答

倚天杖
TA貢獻1828條經(jīng)驗 獲得超3個贊
根據(jù)您的評論:
我的示例中的最后兩行代碼。如果丟失,更改將不會寫入 firestore。據(jù)我了解,是否保存batch.commit()的返回值應(yīng)該是無關(guān)緊要的。
如果缺少這兩行代碼,更準確地說是帶有 的代碼commit()
,則不會發(fā)生任何事情,因為您沒有向批處理提交任何內(nèi)容。是的,你是對的,無論你是否將結(jié)果保存batch.commit()
到result
對象中,commit()
方法都將始終在該對象上被調(diào)用。因此,只有當您調(diào)用批處理對象時,才會將批處理寫入Firestore中commit()
,否則之前存在的代碼行是無用的。
添加回答
舉報
0/150
提交
取消