从Firebase数据库更新数组值

时间:2019-02-09 03:10:30

标签: ios arrays swift firebase-realtime-database

我有静态tableview,它使用此数组返回数组计数

var profiles = [
    Profile(title: "Email", description: "Add Email"),
    Profile(title: "Gender", description: "Add Gender"),
    Profile(title: "Location", description: "Add Location"),
    Profile(title: "About", description: "Add About")
]

我想根据从Firebase数据库获得的数据更新数组中的描述,但是当我想要更新时,它不在控制器中显示,这是我的代码

fileprivate func updateUserProfile() {
    guard let uid = Auth.auth().currentUser?.uid else { return }

    Database.database().reference().child("users").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in
        guard let value = snapshot.value as? [String: Any] else { return }

        guard let emailDescription = value["email"] as? String else { return }
        guard let genderDescription = value["gender"] as? String else { return}

        self.profiles[0].description = emailDescription
        self.profiles[1].description = genderDescription

    }) { (error) in
        print(error.localizedDescription)
    }
}

如何从Firebase数据库更新阵列?

1 个答案:

答案 0 :(得分:0)

从Firebase获取更新值后,您应该重新加载数据。将其放入observeSingleEvent完成中:self.tableView.reloadData()