firestore 查询返回空计数但实际上有文档

时间:2021-07-18 19:08:52

标签: ios swift firebase google-cloud-firestore uialertcontroller

所以我的目标是修复我的 Firestore 查询功能以返回正确的结果。目前我有一个功能,可以在删除之前检查学生是否购买了活动的门票。如果有学生已经购买了门票,则无法从数据库中删除该事件,以防止发生错误和崩溃。如果学校用户尝试删除活动,则应显示警告,说明由于上述原因他们无法删除活动。

我在 2 个月前使用完全相同的代码启动了该应用程序,它运行良好,但今天突然在我拍摄演示文稿时,我的代码在我眼前基本上失败了。

这是函数:

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

    let deleteAction = UIContextualAction(style: .destructive, title: "Delete") { (deleted, view, completion) in
        
        guard let user = Auth.auth().currentUser else { return }
        let deleteIndex = client.index(withName: IndexName(rawValue: user.uid))
        let documentid = self.documentsID[indexPath.row].docID
        let algoliaID = self.algoliaObjectID[indexPath.row].algoliaObjectID
        
        
        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 { return }
                
                for document in querySnapshot!.documents {
                    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(error)
                            return }
                        guard let query = querySnapshotTwo?.isEmpty else { return }
                        if query == false {
                            self.showAlert(title: "Students Have Purchased Tickets For This Event", message: "This event cannot be deleted until all students who have purchased a ticket for this event have completely refunded their purchase. Please be sure to make an announcement that this event will be deleted.")
                            return
                        } else {
                            print(querySnapshotTwo?.count)
                            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
                                let batch = self.db.batch()
                                let group = DispatchGroup()
                                group.enter()
                                deleteIndex.deleteObject(withID: ObjectID(rawValue: algoliaID)) { (result) in
                                    if case .success(_ ) = result {
                                        group.leave()
                                    }
                                }
                                
                                group.notify(queue: .main) {
                                    let docRef = self.db.document("school_users/\(user.uid)/events/\(documentid)")
                                    batch.deleteDocument(docRef)
                                    batch.commit { (err) in
                                        guard err == nil else { return }
                                    }
                                    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
    
}

所以基本上发生的事情是我发送第一个查询以检查该学校 ID 下的学生,如果没有,则删除过程可以正常进行。如果是,则第二个查询检查这些学生是否购买了此活动,并通过将购买的 eventID 与发布时该活动的文档 ID 进行匹配来进行此查询。我现在将展示一些图片来解释我的意思:

eventID

因此,如果您查看数据库中的 eventID 字段,您将看到该数字,这是我与学校用户发布事件的文档 ID 匹配的数字。这是一张图片:

documentID

因此,您显然可以看到两个数字匹配,这不应返回空查询,因为有学生购买了此活动的门票。现在,当我滑动该特定事件的单元格并点击删除时,会弹出正常的删除警报,并且查询计数在控制台中打印 0,这是不应该发生的。

console

有人知道如何解决这个查询问题吗?

EDIT 如果用户 ID 是硬编码的,这可以正常工作,但显然我不能对路径中的多个用户进行硬编码。

0 个答案:

没有答案