0

I'm developing an Android Q&A application. I'd like users to delete all of their posts when they delete their accounts. I found this article so I tried the same code, but it didn't work. This is my code.

@Composable
fun DeleteAccounts(navController: NavController, uid: String) {
    val db = Firebase.firestore

    Button(
                    onClick = {
                        val batch = db.batch()
                        db.collection("posts")
                            .whereEqualTo("uid", "$uid")
                            .get().result.forEach {
                                batch.delete(it.reference)
                            }
                        batch.commit()
                            .addOnSuccessListener {
                                navController.navigate("Login")
                            }
                    }

And this is the error message.

java.lang.IllegalStateException: Task is not yet complete
    at com.google.android.gms.common.internal.Preconditions.checkState(com.google.android.gms:play-services-basement@@18.1.0:2)
    at com.google.android.gms.tasks.zzw.zzf(com.google.android.gms:play-services-tasks@@18.0.1:1)
    at com.google.android.gms.tasks.zzw.getResult(com.google.android.gms:play-services-tasks@@18.0.1:1)
    at com.example.**app.DeleteAccountsViewKt$DeleteAccounts$1$5$1$1.invoke(DeleteAccountsView.kt:124)
    at com.example.**app.DeleteAccountsViewKt$DeleteAccounts$1$5$1$1.invoke(DeleteAccountsView.kt:117)

What am I doing wrong? Thank you.

I tried another code and this works. But I don't know if this is correct.

val batch = db.batch()
                        db.collection("posts")
                            .whereEqualTo("uid", uid)
                            .get()
                            .addOnSuccessListener { result ->
                                for (document in result) {
                                    batch.delete(document.reference)
                                }
                  
              batch.commit()
Yuki
  • 225
  • 2
  • 7
  • Have you tried Micro's recommendation? – Alex Mamo Nov 14 '22 at 09:10
  • I considered to use Cloud Function, but for now, I'd like to go forward without it. I saw many articles to delete multiple documents from Firestore but I only can understand Kotlin and I'm still struggling with it. – Yuki Nov 15 '22 at 06:18

1 Answers1

1

It's possible there are too many documents to delete in your Android client. If you read the documentation here: https://firebase.google.com/docs/firestore/manage-data/delete-data#collections It says that batch deleting documents in a client is not recommended.

You could try writing a Cloud Function to batch delete a lot of documents instead.

Micro
  • 10,303
  • 14
  • 82
  • 120
  • Thank you for the information. Yes, the document says " Deleting collections from an Android client is not recommended." but what I want to do is to delete chosen documents in a collection. For my case, isn't batch deleting recommended, either? – Yuki Nov 15 '22 at 07:32
  • 1
    @Yuki If you have maybe a dozen or so documents to delete at once, you could do a loop and delete them one by one using https://firebase.google.com/docs/firestore/manage-data/delete-data#delete_documents. But if you are deleting a lot of documents, you would be wise to follow what the documentation says and do that in a Cloud Function or some other backend service. – Micro Nov 15 '22 at 22:06
  • I see. My case applies to the former one, I suppose. Not hundreds but a dozen/dozens. Still I don't know what's wrong with the code above. Actually, I tried another code(also above) and it works, but I'm not sure if it is correct. – Yuki Nov 15 '22 at 22:40
  • 1
    @Yuki Your second code looks correct. And if it works, then that is even better. – Micro Nov 16 '22 at 01:16