我正在尝试在数据库事务中执行两项操作。
还检查了是否成功调用了成功侦听器,但文档字段未更新。文档引用的路径也正确。
database.runTransaction((Transaction.Function<Void>) transaction -> {
message.createdAt = new Timestamp(new Date()); // just to be safe if the UI is not creating this.
collRef.add(ModelMapper.prepareMessageModel(message)).addOnCompleteListener(task -> {
// Added a new document in collection A
});
CollectionReference collRefConf = composer.composeConversationCollectionReference(conversationRequest);
collRefConf.get(Source.SERVER).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
// Updating an existing document in collection B
ConversationConfigRequest configRequest = new ConversationConfigRequest(message.conversationDocumentId,
document.getId());
DocumentReference configDocReference = composer.composeConversationConfigDocumentReference(configRequest);
configDocReference.update(
FirestoreConstants.ConversationProperties.ConversationConfig.LAST_MESSAGE, message.message
)
.addOnSuccessListener(aVoid -> {
Log.d(Config.LOGTAG, "onSuccess: ");
})
.addOnFailureListener(e -> Log.d(Config.LOGTAG, "onFailure: "+e));
}
}
}).addOnFailureListener(e -> {
Log.d("", "pullConversationConfigId: ");
});
return null;
});
答案 0 :(得分:0)
如果将更改整个对象,则可以尝试使用set以便更新整个文档。还可以尝试使用OncompleteListener而不是onSuccessListener来查看任务是否已完成
FirebaseFirestore.getInstance().collection().document(id)
.set(yourObject).addOnCompleteListener{/*your code after completion here*/}