Home > Software engineering >  Deleting a document on Firebase Firestore using its autoID
Deleting a document on Firebase Firestore using its autoID

Time:11-27

I made an automobile management application.

After the user registers with his e-mail address and logs in, he adds a vehicle and enters other information under it.

The license plates of these vehicles are displayed in a recycler view.

Clicking on the plate takes you to a screen that shows the information it has filled in.

I put a delete button on this screen.

However, as I have shown in my code, how can I show the auto-generated ID of the opened page to delete it?

  1. Page showing plates plates page
  2. The page that contains the clicked plate and the delete button (with a red frame) info page
  3. My Firebase Firestore min display Firebase Firestore

I don't know how to code.

I'm trying to watch video lessons and adapt them the way I want to make the applications in my head.

This application is the last point I came to.

As far as I saw the answers to the previous questions and the videos, I could not answer this question.

I know it will be difficult, but can you show this ignorant the correct method?

public void deleteCarsClicked(View view) {
    firebaseFirestore.collection("Arabalar").document("How do I show the ID of the opened page here.")
            .delete()
            .addOnSuccessListener(new OnSuccessListener<Void>() {
                @Override
                public void onSuccess(Void unused) {
                    Intent intent = new Intent(RstActivity.this, CarsActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(RstActivity.this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
                }
            });
}

CodePudding user response:

As @FrankvanPuffelen mentioned in his comment, to be able to delete a particular document, you need to know its document ID. For example, if you have the following schema:

db-root
 |
 --- licensePlates (collection)
      |
      --- $licensePlateId (document)
            |
            --- code: "34FHR147"
            |
            --- licensePlateId: "FirestoreDocumentId"

To delete this document, you call delete() as you can see below:

FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("licensePlates")
  .document("FirestoreDocumentId")
  .delete()
  .addOnCompleteListener(/*.../*);

If you however don't know the "FirestoreDocumentId", then you have to perform a query:

Query queryByLicensePlate = db.collection("licensePlates").whereEqualTo("code", "34FHR147");
queryByLicensePlate.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                document.getReference().delete().addOnCompleteListener(new OnCompleteListener<Void>() {
                    @Override
                    public void onComplete(@NonNull Task<Void> deleteTask) {
                        if (deleteTask.isSuccessful()) {
                            Log.d(TAG, "Document successfully deleted.");
                        } else {
                            Log.d(TAG, "Error deleting document: ", deleteTask.getException());
                        }
                    }
                });
            }
        } else {
            Log.d(TAG, "Error getting documents: ", task.getException());
        }
    }
});
  • Related