Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios Firestore文档和Algolia记录有时仅删除_Ios_Swift_Firebase_Google Cloud Firestore_Algolia - Fatal编程技术网

Ios Firestore文档和Algolia记录有时仅删除

Ios Firestore文档和Algolia记录有时仅删除,ios,swift,firebase,google-cloud-firestore,algolia,Ios,Swift,Firebase,Google Cloud Firestore,Algolia,因此,我的目标是基本上拥有一个Firestore文档和一个Algolia记录,当我第一次点击delete按钮时,Firestore文档将被删除,而不必担心删除两次它才能正常工作 这是我目前在删除文档和Algolia记录时使用的功能: override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeAction

因此,我的目标是基本上拥有一个Firestore文档和一个Algolia记录,当我第一次点击delete按钮时,Firestore文档将被删除,而不必担心删除两次它才能正常工作

这是我目前在删除文档和Algolia记录时使用的功能:

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
    
}
所以这个函数有时会起作用。当我创建一个事件时,它会在Algolia索引中为用户创建一个Firestore文档和一个Algolia记录;现在,假设我以学校用户的身份发布了一个事件,但随后决定不久将其删除。如果我在单元格上滑动,按delete并接受警报提示,
组中的代码。notify
打印到控制台,而不是错误消息,然后是成功消息

在控制台中看到该事件,以及单元格从tableview中完全消失后,您可以假设该事件已从Firestore数据库和Algolia索引中删除。我有一个搜索栏来搜索我的Algolia记录,如果我在删除它之后立即搜索该事件,它仍然会显示在点击中。现在如果我点击它,你可能会认为该应用程序可能会崩溃,因为Firestore文档被删除,没有可用的数据显示,完全相反的情况发生了;所有正确的数据都显示为事件未被删除,如果我退出显示事件数据的vc并返回到tableview,它将神奇地再次出现


现在,如果重复我提到的相同的删除过程(滑动单元格…,等等),成功消息将打印出来,文档将在数据库中以及Algolia索引中完全删除。如何修复此错误以确保事件在Cloud Firestore和Algolia中第一次正确删除?

此问题仅在创建事件后或整个时间内发生?