Firestore级联删除具有相同已删除ID的每个项目

时间:2018-08-05 08:32:46

标签: android firebase kotlin google-cloud-firestore

删除项目时遇到问题。

Subject list

删除主题时,我要删除与主题具有相同ID的任务

Task list

我尝试过

mFirestore.collection("classes").document(classID).collection("subjects").document(subjectID).delete().addOnCompleteListener {
     mFirestore.collection("classes").document(classID).collection("tasks").whereEqualTo("subject", subjectID).get().addOnCompleteListener{ task ->
        if (task.isSuccessful) {
            for (document in task.result)
                mFirestore.collection("classes").document(classID).collection("tasks").document(document.id).delete()
        }
    }
}

但没有成功。

现在,我想使用Firebase函数执行此操作。我怎样才能做到这一点? Java或Kotlin中的代码相同

1 个答案:

答案 0 :(得分:2)

尝试一下:-

mFirestore.collection("classes").document(classID).collection("subjects").document(subjectID)
        .delete()
        .addOnSuccessListener(new OnSuccessListener<Void>() {
            @Override
            public void onSuccess(Void aVoid) {
                mFirestore.collection("classes").document(classID).collection("tasks").whereEqualTo("subject", subjectID).get().addOnCompleteListener(this, new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(Task<QuerySnapshot> task) {
            if (task.isSuccessful())
            {
                for (QueryDocumentSnapshot documentSnapshot : task.getResult())
                {
                mFirestore.collection("classes").document(classID).collection("tasks").document(documentSnapshot.getId()).delete();
                }
            }
        }
    });
     }
        })
        .addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                Log.w(TAG, "Error deleting document", e);
            }
        });