如何在swift中从Firebase Firestore中删除当前用户的特定文档?

问题描述 投票:0回答:1

我正在使用 Firebase Firestore 来存储任务集合。我想创建以下场景:当用户删除一个特定的任务时,它应该从表视图和数据库中删除,只针对当前这个用户。但我只能从数据库中删除所选的任务,而不能删除当前用户的任务,请帮助我。

我的设置代码。

class TasksListScreen: UIViewController {
    var tasksArray = [Task]() // array of tasks via Task struct

    // to load data from db
    func loadData() {
        db.collection("tasks").getDocuments { (queryShapshot, error) in
            if let error = error {
                print("\(error.localizedDescription)")
            } else {
                self.tasksArray = queryShapshot!.documents.flatMap({Task(dictionary: $0.data())})
                // to update user interface
                DispatchQueue.main.async {
                    self.tableView.reloadData()
                }
            }
        }
    }
}

我可以通过这段代码从我的收藏中删除所选的任务(文档),但我需要从数据库中删除所选的文档。

extension TasksListScreen: UITableViewDataSource, UITableViewDelegate {

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

        // if action is deletion
        if editingStyle == UITableViewCell.EditingStyle.delete {

            let title = tasksArray[indexPath.row].title
            let collectionRef = db.collection("tasks")
            let query : Query = collectionRef.whereField("title", isEqualTo: title)

            query.getDocuments(completion:{ (snapshot, error) in
                if let error = error {
                    print(error.localizedDescription)
                } else {
                    for document in snapshot!.documents {
                        self.db.collection("tasks").document("\(document.documentID)").delete()
                    }
                }
            })

            // delete task from table view
            tasksArray.remove(at: indexPath.row)
            //tableView.reloadData()
            tableView.deleteRows(at: [indexPath], with: .fade)
        }
}

但我需要从db中删除选中的文档,只针对当前用户。我试过这段代码。

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

        // if action is deletion
        if editingStyle == UITableViewCell.EditingStyle.delete {

            // delete task from db for the current user
            let title = tasksArray[indexPath.row].title
            let user = Auth.auth().currentUser
            // access collection of tasks of the current user
            let collectionRef = db.collection("users").document((user?.uid)!).collection("tasks")
            // search for task with needed title
            let query : Query = collectionRef.whereField("title", isEqualTo: title)
            print(title)
            print(query)
            query.getDocuments(completion:{ (snapshot, error) in
                if let error = error {
                    print(error.localizedDescription)
                } else {
                    for document in snapshot!.documents {
                        self.db.collection("users").document((user?.uid)!).collection("tasks").document("\(document.documentID)").delete()
                    }
                }
            })
        }
 }

它在当前会话期间删除了任务,但当我再次运行我的应用程序时,被删除的任务就会出现。

请帮助

ios swift firebase uitableview google-cloud-firestore
1个回答
0
投票

如果文档中有一个包含userId的字段,你可以这样做。

for document in snapshot!.documents {
     if document.userId == user.userId {
         self.db.collection("tasks").document("\(document.documentID)").delete()
}
               }
© www.soinside.com 2019 - 2024. All rights reserved.