[英]Delete documents of cloud firestore collection after I close the app
So I have a collection in Cloud Firestore which I populate by clicking some buttons in my application.所以我在 Cloud Firestore 中有一个集合,我通过单击应用程序中的一些按钮来填充它。 Once I close the app, I want the collection to be empty.
关闭应用程序后,我希望集合为空。 Is there any way I can do that?
有什么办法可以做到吗? I tried to implement onStop() method, but it does nothing.
我试图实现 onStop() 方法,但它什么也没做。
protected void onStop() {
super.onStop();
db.collection("AddPlaces").document()
.delete()
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Log.d("SUCCES", "DocumentSnapshot successfully deleted!");
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w("FAILURE", "Error deleting document", e);
}
});
}
Any ideas if I can achieve that?如果我能做到这一点,有什么想法吗?
An empty collection means a collection with no documents.空集合意味着没有文档的集合。 If you want to delete all documents in a collection, I recommend you see my answer in the following post:
如果您想删除集合中的所有文档,我建议您在以下帖子中查看我的答案:
Why your code is not working?为什么你的代码不起作用? Is because of the following line of code:
是因为下面这行代码:
db.collection("AddPlaces").document()
Which basically generates a document with a random ID, and nothing more.这基本上会生成一个带有随机 ID 的文档,仅此而已。 So when you call delete(), you're trying to delete a single document and not all documents in the collection, hence that behavior.
因此,当您调用 delete() 时,您是在尝试删除单个文档,而不是集合中的所有文档,因此会出现这种行为。
One more thing to note is that if you delete all documents in the collection, that collection will not exist anymore.另外需要注意的是,如果您删除集合中的所有文档,则该集合将不再存在。 It will exist again when you'll write a new document in it.
当您在其中编写新文档时,它将再次存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.