uitableview 单元格第一次仍然没有正确删除

uitableview cell still doesn't delete properly the first time

所以我的目标是能够一次性删除表格视图单元格,而不必担心尝试删除两次。我有一种方法可以从 tableview 中删除单元格,此外还可以从 Algolia 索引中删除记录,从 Firestore 集合中删除文档。

方法如下:

 override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

    let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (deleted, view, completion) in
        let alert = UIAlertController(title: "Delete Event", message: "Are you sure you want to delete this event?", preferredStyle: .alert)
        
        let deleteEvent = UIAlertAction(title: "Delete", style: .destructive) { (deletion) in
            guard let user = Auth.auth().currentUser else { return }
            let documentid = self.documentsID[indexPath.row].docID
            let algoliaID = self.algoliaObjectID[indexPath.row].algoliaObjectID
            let deleteIndex = client.index(withName: IndexName(rawValue: user.uid))
            
            self.getTheSchoolsID { (id) in
                guard let id = id else { return }
                self.db.collection("student_users").whereField("school_id", isEqualTo: id).getDocuments { (querySnapshot, error) in
                    guard error == nil else {
                        print("Couldn't fetch the student users.")
                        return
                    }
                    let group = DispatchGroup()
                    for document in querySnapshot!.documents {
                        group.enter()
                        let userUUID = document.documentID
                        self.db.collection("student_users/\(userUUID)/events_bought").whereField("eventID", isEqualTo: documentid).getDocuments { (querySnapshotTwo, error) in
                            guard error == nil else {
                                print("Couldn't fetch if student users are purchasing this event")
                                return
                            }
                            guard querySnapshotTwo?.isEmpty == true else {
                                self.showAlert(title: "Students Have Purchased This Event", message: "This event cannot be deleted until all students who have purchased this event have completely refunded their purchase of this event. Please be sure to make an announcement that this event will be deleted.")
                                return
                            }
                            group.leave()
                    }
                        
                    }
                    
                    group.notify(queue: .main) {
                        
                        deleteIndex.deleteObject(withID: ObjectID(rawValue: algoliaID)) { (result) in
                            if case .success(let response) = result {
                                print("Algolia document successfully deleted: \(response.wrapped)")
                            }
                        }


                        self.db.document("school_users/\(user.uid)/events/\(documentid)").delete { (error) in
                            guard error == nil else {
                                print("There was an error deleting the document.")
                                return
                            }
                            print("Deleted")
                        }
                        self.eventName.remove(at: indexPath.row)
                        tableView.reloadData()
                    }
                }
            }
        }
        
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
        alert.addAction(deleteEvent)
        self.present(alert, animated: true, completion: nil)
    }
    
    deleteAction.backgroundColor = UIColor.systemRed

    
    let config = UISwipeActionsConfiguration(actions: [deleteAction])
    config.performsFirstActionWithFullSwipe = false
    return config
    
}

非常简单,我最近添加了 DispatchGroup 块来修复一些 UI 问题以及 Firestore 查询,但除此之外它就是之前 group.notify() 中的内容那。

现在由于某种原因,当我尝试删除单元格时,它会删除并显示打印消息,就好像删除成功一样,但是当我在我放置在视图控制器中的搜索栏中搜索时,我可以仍会在搜索结果中看到该单元格。这基本上意味着不仅记录没有从 Algolia 中删除,而且 Firestore 中的文档也没有被删除。 是的,删除后单元格已从表视图中删除,但只要我搜索并点击它,它就会实例化视图控制器并显示 Firestore 文档中的所有字段。一旦我点击该视图控制器,该单元格就会重新出现在表视图中,并要求我再次删除它才能使其实际工作。

所以问题是,使用我提供的功能,如何确保第一次正确删除单元格?

根据@dante 的评论,描述中的方法目前有效,但是:

  • 目前,上面的代码会在这些功能中的任何一个完成之前重新加载 table 视图。

  • 如@jnpdx 所述,正确的方法是等待删除结束后再重新加载 table 视图,因为 deleteIndex.deleteObjectdocument(...).delete 都是异步函数。