ios - Firebase updateChildValues 函数会阻止其他 Firebase 函数

标签 ios swift firebase firebase-realtime-database

我正在使用 updateChildValues 函数更新数据库中 500 名学生的值,当我触发它并尝试触发另一个 Firebase 函数(如观察)时,第二个函数(观察)不执行并一直等到第一个操作(updateChildValues ) 完成,这需要将近 15 秒。

这是我的 updateChildValues 代码:

@IBAction func menu(_ sender: Any) {
        let storeRecord = UIAlertAction(title: "Store attendance record", style: .default) { action in
            var attendanceTimesDict = [String: Int]()
            var absenceTimesDict = [String: Int]()
            for student in self.students { // 500 students
                if self.attendanceRecord[student.identifier] == true {
                    student.attendanceCount += 1
                    attendanceTimesDict["\(student.identifier)/attendance"] = student.attendanceCount
                } else {
                    student.absenceCount += 1
                    absenceTimesDict["\(student.identifier)/absence"] = student.absenceCount
                }
            }
            Database.database().reference().child("class").child("CS101").child("records").child("27-4-2019").updateChildValues(self.attendanceRecord)
            Database.database().reference().child("class").child("CS101").child("students").updateChildValues(attendanceTimesDict)
            Database.database().reference().child("class").child("CS101").child("students").updateChildValues(absenceTimesDict)
        }
}

这是我的观察函数代码:

func getStudentsList() {
        Database.database().reference().child("class").child("CS101").child("students").observeSingleEvent(of: .value) { snapshot in
            for child in snapshot.children.allObjects as! [DataSnapshot] {
                let studentObject = child.value as? [String: Any]
                if let name = studentObject?["name"] as? String {
                    let student = Student(identifier: child.key, name: name)
                    self.students.append(student)
                }
            }
          self.tableView.reloadData()
        }
}

最佳答案

Firebase 实时数据库服务器一次执行一个操作,以确保它始终返回一致的状态。

如果您想防止单个大型写入操作长时间阻塞其他操作,您可以将该大型操作拆分为多个部分。

关于ios - Firebase updateChildValues 函数会阻止其他 Firebase 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55913288/

相关文章:

ios - SiriKit INPayBillIntentHandling - Siri 说, "I wish I could, but <App> hasn' t 还没有和我一起设置。”

ios - 使用 Swift 如何将字符串转换为数字

ios - 将 UIView 的大小调整为其 UILabel subview 的大小

ios - bool 字段 UIAlertController swift

java - Firebase 与 sqlite 一起

ios - 使用默认的 MonoTouch 解决方案会产生构建错误

ios - 尝试访问 ios 时,sqlite 预填充数据库为空

ios - 如何同时使用 swift 2 和 swift 3?

ios - Swift 3.0 如何在 firebase 的父对象上添加子对象

firebase - 是否可以在单个控制台内创建 Firestore 数据库的多个实例?