【问题标题】:Swift firestore delete documentSwift Firestore 删除文档
【发布时间】:2019-09-15 22:03:53
【问题描述】:

我想从我的 Firestore 数据库中删除一个文档。但我的代码只会让我删除所有文档。不知何故,我需要获取与我正在滑动的 tableview 中的行相对应的 DocumentID。有什么建议么?

我的代码是这样的;

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()

                    }
                }}}

【问题讨论】:

  • 你知道文档id吗?
  • 快照获取所有文档ID。但我不知道如何将 tableview 中的选定行引用到战斗文档 ID

标签: swift google-cloud-firestore document


【解决方案1】:

当您从 Firestore 加载文档时,您需要维护一个文档 ID 列表,以与您在表格视图中显示的值列表相匹配。然后你可以在用户滑动值时查找文档ID,并删除文档:

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

如果您正在寻找有关如何做到这一点的灵感,请查看FirebaseUI implementation of its data sources。那里的FUIBatchedArray 保留了从Firestore 获得的documents,然后您可以使用get by their index,TableViewDataSource 然后将其用于implement cellForRowAtIndexPath。

【讨论】:

    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 2021-07-01
    • 2020-09-01
    • 1970-01-01
    • 2021-10-16
    • 2020-10-28
    相关资源
    最近更新 更多