【问题标题】:Delete a document from Firestore从 Firestore 中删除文档
【发布时间】:2020-09-01 00:28:02
【问题描述】:

我正在尝试从名为“photo”的集合中删除一个文档,但它不起作用,没有 OnFailureException 消息,因为显示了 OnSuccess Toast 但该文档仍保留在 Firestore 中:(

Firestore 的结构:

这是我用来删除文档的代码:

        Photo photo = (Photo) getIntent().getSerializableExtra("photo");

        FirebaseFirestore db = FirebaseFirestore.getInstance();

        String userId = mAuth.getCurrentUser().getUid();

        CollectionReference photoRef = db.collection("main").document(userId).collection("photo");

DocumentReference document = photoRef.document(photo.getId());
                    String currentDocumentID = document.getId();
                    photoRef.document(currentDocumentID).delete().addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Toast.makeText(ViewPhotoActivity.this, "Entry Deleted", Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(ViewPhotoActivity.this, PhotoActivity.class));
                            finish();
                        }
                    }).addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Toast.makeText(ViewPhotoActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
                        }
                    });

【问题讨论】:

    标签: android google-cloud-firestore


    【解决方案1】:

    我正在根据猜测写答案,因为没有足够的信息来说明您是如何创建文档的。

    DocumentReference document = photoRef.document(photo.getId());
    

    在上述行中,您正在创建对带有照片 ID 的文档的引用。查看文档数据,似乎 照片 ID 与文档 ID 不同

    因此,当您创建上述参考时 - 您不是使用 id="2BMG3..." 引用现有文档,而是使用 id="jYBPX..." 引用新文档

    String currentDocumentID = document.getId();
    photoRef.document(currentDocumentID).delete()
    

    如何修复它取决于您希望如何保存/创建文档。一种方法是首先使用id= photo idphoto 集合中创建文档。

    例如,在创建时,您可以按如下方式创建文档引用:

    CollectionReference photoRef = db.collection("main")
                                    .document(userId)
                                    .collection("photo")
                                    .document(photo.getId());
    

    然后将数据设置为:

    photoRef.set(photo);
    

    这会将数据设置为您使用id=photo id 创建的文档。

    如果您现在展示如何将文档添加到photo 集合,这些示例可以得到改进。如果您一直在使用.colection('photo').add().collection.document().set(photo),则创建的文档将具有与照片 ID 不同的自动生成 ID。

    【讨论】:

    • 嗨,你能先告诉我如何设置 id = photo id 吗?
    • @Lionlollipop 用示例更新了答案,如果仍然不清楚,请添加一些关于您如何创建文档的 sn-p,然后示例可能更相关。
    • 感谢您的回答,非常有用:)
    【解决方案2】:

    我确信有几种方法可以做到这一点,其中包括我在上面收到的答案,getID 方法让我有点困惑,因为它有时不起作用或给我一个空异常,所以我更改了我的代码并使用更简单的方法,即在创建每个文档时为其创建一个唯一 ID,然后使用该唯一 ID 设置文档 ID,以便我可以轻松检索它们,对于这种情况,我通过组合标题和6个随机数字。

    创建文档时,我为每个文档添加了唯一的 ID 字段:

    //set a unique ID for each photo
                    String randomSixNumbers = getRandomNumberString();
                    String photoId = (title.toLowerCase() + randomSixNumbers).replace(" ", "").trim();
    
    //set ID to new object 
                  Photo photo = new Photo(photoId, title, photoDescription, filePath, datePhoto);
    
    //add object to firestore and set document ID as the uniqueID instead of the default auto-generated documentID                 
                  photoRef.document(photoId).set(photo).addOnSuccessListener(...
    

    使用唯一 ID 删除文档:

    photoRef.document(photo.getId()).delete().addOnSuccessListener(...
    

    目前,这个过程似乎还可以,但我不确定这是否是一个好习惯?如果没有,有人可以指出这种方法的缺点/错误吗?

    我注意到我必须使用意图来获取从上一个活动传递的数据,以获取当前活动中的对象数据。

    【讨论】:

      猜你喜欢
      • 2021-10-16
      • 1970-01-01
      • 2021-01-13
      • 2020-11-23
      • 1970-01-01
      • 2018-05-08
      • 2020-09-11
      • 2019-02-03
      • 1970-01-01
      相关资源
      最近更新 更多