Android:无法更新Firestore中的文档字段

时间:2020-02-26 20:23:12

标签: android google-cloud-firestore

我正在尝试在数据库事务中执行两项操作。

  • 在集合A中添加新文档。成功
  • 更新集合B中现有文档的字段。失败

还检查了是否成功调用了成功侦听器,但文档字段未更新。文档引用的路径也正确。

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;
        });

1 个答案:

答案 0 :(得分:0)

如果将更改整个对象,则可以尝试使用set以便更新整个文档。还可以尝试使用OncompleteListener而不是onSuccessListener来查看任务是否已完成

FirebaseFirestore.getInstance().collection().document(id)
    .set(yourObject).addOnCompleteListener{/*your code after completion here*/}