简体   繁体   English

关闭应用程序后删除云火库集合的文档

[英]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.

相关问题 如何使用 Flutter 删除 Firestore 中集合中的所有文档 - How to Delete all documents in collection in Firestore with Flutter 无法删除 Firebase Cloud Firestore 中的多个文档 - Cannot delete multiple documents in Firebase Cloud Firestore 如何从 Cloud Firestore 中删除多个文档? - How to delete multiple documents from Cloud Firestore? 如何检查 Cloud Firestore 中任何文档的集合中是否存在值(例如名称)? - How can I check if a value e.g. name exists in a collection within any of documents in Cloud Firestore? 如何使用模块 Web SDK 的版本 9 获取 Cloud Firestore 集合中的所有文档? - How do I get all documents in a Cloud Firestore collection using Version 9 of the Modular Web SDK? Cloud Firestore:侦听集合中的新文档并加载它们 - Cloud Firestore: listen for new documents in a collection and load them cloud firestore 快照是否读取集合中的所有文档? - Do cloud firestore snapshot reads all the documents in the collection? FLUTTER:如何在cloud firestore 中使用where 子句删除多个文档? - FLUTTER: How to delete multiple documents using where clause in cloud firestore? 如何根据 Cloud Firestore 中的主集合字段和子集合字段查询子集合中的文档? - How to query documents from a sub-collection, based on a main collection's field and a sub-collection's fields in cloud firestore? Cloud Firestore 集合计数 - Cloud Firestore collection count
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM