【问题标题】:Firebase updateChildValues function blocks other Firebase functionsFirebase updateChildValues 函数会阻止其他 Firebase 函数
【发布时间】:2019-04-30 02:42:21
【问题描述】:

我正在使用 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()
        }
}

【问题讨论】:

    标签: ios swift firebase firebase-realtime-database


    【解决方案1】:

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

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

    【讨论】:

    • 我尝试了 setValue 而不是 updateChildValues,但它冻结了应用程序。
    • 使用相同数据调用setValueupdateChildValues 之间没有显着的性能差异。为了有所作为,您必须将大型操作拆分为多个 API 调用。
    • 你能告诉我怎么做吗?
    猜你喜欢
    • 2020-09-02
    • 1970-01-01
    • 2018-10-19
    • 2013-06-17
    • 1970-01-01
    • 1970-01-01
    • 2016-05-24
    • 2021-08-10
    • 1970-01-01
    相关资源
    最近更新 更多