简体   繁体   English

Swift Firestore删除文件

[英]Swift firestore delete document

I would like to delete a document from my firestore DB. 我想从Firestore数据库中删除文档。 But my code only makes me delete all documents. 但是我的代码仅使我删除所有文档。 Somehow i need to get DocumentID corresponding to the line in the tableview I'm swiping. 不知何故,我需要获取与我要滑动的表格视图中的行相对应的DocumentID。 Any suggestions? 有什么建议么?

My code looks like this; 我的代码看起来像这样;

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {


        self.db.collection("Miljoskjema").getDocuments { (snapshot, err) in

            if let err = err {
                print("Error getting documents: \(err)")
            } else {
                for document in snapshot!.documents {

                    document.reference.delete()

                    }
                }}}

When you load the documents from Firestore, you'll need to maintain a list of the document IDs to match up with the list of values that you show in the table view. 从Firestore加载文档时,您需要维护一个文档ID列表,以与在表格视图中显示的值列表匹配。 Then you can look up the document ID when the user swipes a value, and delete the document with: 然后,当用户滑动值时,您可以查找文档ID,并使用以下命令删除文档:

self.db.collection("Miljoskjema").doc(documentId).delete()

If you're looking for inspiration on how to do that, have a look at the FirebaseUI implementation of its data sources . 如果您在寻求灵感的方法,请查看FirebaseUI数据源的实现 The FUIBatchedArray in there keeps the documents it gets from Firestore, which you can then get by their index , which the TableViewDataSource then uses to implement cellForRowAtIndexPath . 那里的FUIBatchedArray保留了从Firestore获取的文档 ,然后可以通过它们的索引获取这些索引 ,然后TableViewDataSource使用这些索引实现cellForRowAtIndexPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM