0

I'm trying to delete documents from firestore using:

 FirebaseFirestore.instance
      .collection("Data")
      .where('user_id', isEqualTo: uid)
      .get()
      .then((snapshot) {
    for (DocumentSnapshot ds in snapshot.docs) {
      ds.reference.delete().then((value) {
        print("document deleted");
        moveToNextDeletion(); // <-The function shold be called only once
      });
    }
  });

so once all documents deleted I wanna call moveToNextDeletion(); function, but it keeps calling until all documents deleted (cause of for loop).

Is there any way to call that function once all documents deleted?

Pranav
  • 411
  • 6
  • 19

2 Answers2

0

Use while loop instead of for loop hope it will work

Osama Buzdar
  • 1,115
  • 10
  • 20
0

use .whenCompleted() instead this method triggers when your prescribed operation is completed instead of using then.

Difference between .then() and .whenCompleted() methods when working with Futures?

As far as I know .then() function returns an result from the job of previous function. Here you are doing an deletion operation. So you are not getting any results from the database.

 FirebaseFirestore.instance
      .collection("Data")
      .where('user_id', isEqualTo: uid)
      .get()
      .then((snapshot) {
    for (DocumentSnapshot ds in snapshot.docs) {
      ds.reference.delete().then((value) {
        print("document deleted");
        
      });
    }
  }).whenCompleted(() {
    moveToNextDeletion(); // <-The function shold be called only once
  });

I guess this will solve your issue. If it solves the issue then ping me I will explain

  • Even though if I use `whenCompleted()` it's still inside the for loop, therefore the next function will keep occurring until for loop finishes. – Pranav Dec 30 '20 at 16:31
  • Can you explain me exactly what you want to do as it seems you want to delete one specific document in your database ```ds.reference.delete()``` and this line says something different. I guess you should pass the retrieved values to your reference ```ds.collection('data').doc(ds.id).delete()``` something like this – 50_Seconds _Of_Coding Dec 30 '20 at 16:51
  • I wanna delete every document inside a `QuerySnapshot` so I'm calling a for loop to "read each document one by one to delete the reference of that documents", **The problem isn't with deletion!, I wanna call another function once all reference deleted** – Pranav Dec 31 '20 at 03:47
  • ``` FirebaseFirestore.instance .collection("Data") .where('user_id', isEqualTo: uid) .get() .then((snapshot) { for (DocumentSnapshot ds in snapshot.docs) { ds.reference.delete().then((value) { print("document deleted"); }); } }).whenCompleted(() {moveToNextDeletion(); // <-The function shold be called only once});``` try this one – 50_Seconds _Of_Coding Dec 31 '20 at 05:10
  • Thanks man I've got another solution. appreciate your help :) – Pranav Dec 31 '20 at 05:17
  • Can you share the solution ? – 50_Seconds _Of_Coding Dec 31 '20 at 05:25
  • I'm sorry I won't be able to post an answer since this question is marked as 'Already has answers'. long story in short: **I added an increament counter inside the for loop once the counter matches with `document.length` I can call the next function** – Pranav Dec 31 '20 at 05:35