【问题标题】:uitableview cell still doesn't delete properly the first timeuitableview 单元格第一次仍然无法正确删除
【发布时间】:2021-07-17 16:53:55
【问题描述】:

所以我的目标是能够一次性删除表格视图单元格,而不必担心尝试删除两次。我有一种从 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 文档中的所有字段。一旦我敲出该视图控制器,该单元格就会重新出现在 tableview 中,并要求我再次删除它以使其实际工作。

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

【问题讨论】:

  • deleteIndex.deleteObjectdocument(...).delete 都是异步的,对吧?但是,您要在其中任何一个功能完成之前重新加载表格视图。
  • 所以从group.notify() 中取出tableview.reloadData()? @jnpdx
  • 老实说,我从不使用调度组,因为我更喜欢组合或回调处理程序,但我可以告诉你,在重新加载之前你需要等待删除完成,你没有这样做.
  • 有人告诉我使用in this question's answer。由于某种原因,昨天我测试它时,它每次都删除得非常好,比如连续 15 次,所以我不知道这是否是一次故障。我没有使用 Combine 的经验,我可以使用回调处理程序,但现在这似乎工作正常。 @jnpdx
  • 这似乎是对该答案所说的内容的松散解释。 该答案说要进行删除的地方,但它没有说明如何处理这些删除的副作用,我再次肯定是异步的自己发挥作用。

标签: ios swift uitableview google-cloud-firestore grand-central-dispatch


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多