使用多个字段更新firebase数据库(iOS,swift),而不覆盖现有数据

时间:2017-09-23 15:03:28

标签: swift firebase

已经参考更新单个字段而不是多个字段来回答这个问题。

当用户在我的应用程序中注册时,我会向firebase数据库写入一个字段列表。 firebase数据库的根目录是用户,每个用户节点都是指定的唯一用户名。

-users
     -username  
        "name"
        "title"
        "date of birth"
        "location"
        "about"
        "height"

在UpdateDetailsController中是一个用户更新页面,他们可以在这里更新其中一些字段:

 let key = model.ref.child("users").child(currentLoggedInUser).key

     let post = [
                        "name": name.text!,
                        "title": title.text!,
                        "about": about.text! ]
            let childUpdates = ["\(key)": post]

            model.ref.updateChildValues(childUpdates)

虽然用户名保持不变,但使用ref.updateChildValues(childUpdates)会覆盖整个节点,如下所示:

 -users
         -username  
            "name"
            "title"
            "about"

建议将不胜感激!

THX。

1 个答案:

答案 0 :(得分:1)

尝试:

model.ref.child(key).updateChildValues(post)