简体   繁体   English

通过自动生成的文档ID从Firestore中删除文档

[英]Deleting a document from Firestore via Auto-generated document ID

I'm trying to create a Property Rental application on Android using Firebase Firestore. 我正在尝试使用Firebase Firestore在Android上创建一个Property Rental应用程序。 Right now, I'm trying to implement a method to delete a specific document (property) within my collection inside Firestore. 现在,我正在尝试实现一种方法,以删除Firestore中集合中的特定文档(属性)。 I figure it is by referencing the auto-generated ID for that particular document, but I simply couldn't get around it. 我认为是通过引用特定文档的自动生成的ID来实现的,但是我根本无法解决它。

This is how the delete feature should work: 这是删除功能的工作方式:

  1. User clicks on a property item from the RecyclerView 用户从RecyclerView单击一个属性项目
  2. It displays a full profile of that property 它显示该属性的完整配置文件
  3. User clicks the delete button from the top right corner and deletes the property from the Firestore database 用户单击右上角的删除按钮,然后从Firestore数据库中删除该属性。

Here is my code where I'm stucked at: 这是我坚持的代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch(item.getItemId()){
        // The delete button
        case R.id.action_delete_btn:
            // Do this when user clicks on delete button
            Toast.makeText(PropertyProfile.this, "You tried to delete this property", Toast.LENGTH_LONG).show();

            deleteItem(item.getOrder());
            return super.onOptionsItemSelected(item);

        default:
            return false;
    }
}

// Here's my problem
private void deleteItem(int index) {
    firebaseFirestore.collection("Posts")
        .document("[DOCUMENT ID RIGHT HERE!]")
        .delete()
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                Toast.makeText(PropertyProfile.this, "You successfully deleted this property", Toast.LENGTH_LONG).show();
            }
        });
}

In order to use the document id that you are looking for, first you need to store it in a variable. 为了使用您要查找的文档ID,首先需要将其存储在变量中。 When you are adding a document to the database and you are using a call to document() method without passing an argument, a unique id is generated. 当您将文档添加到数据库中并且使用对document()方法的调用而不传递参数时,将生成唯一的ID。 To get that id, you should use the following code: 要获取该ID,您应该使用以下代码:

String documentId = postsRef.document().getId();
yourRef.document(documentId).set(yourModelObject);

In which postsRef is the CollectionReference object of your Posts collection and yourModelObject is the object of your Post class. 其中postsRefPosts集合的CollectionReference对象, yourModelObjectPost类的对象。 I also recommend you store that id, as a property of your Post document. 我还建议您将该ID存储为Post文档的属性。

Once you have this id, you can use in your refence like this: 拥有此ID后,您可以像这样使用:

firebaseFirestore
    .collection("Posts")
    .document(documentId)
    .delete().addOnSuccessListener(/* ... */);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM