【问题标题】:Updating a Realtime Database object in Firebase iOS SDK在 Firebase iOS SDK 中更新实时数据库对象
【发布时间】:2019-04-16 11:50:45
【问题描述】:

我有一个如下所示的数据库:

我想更新第二个用户,即 uid 为 5p3XT8TVFbVYNmFt0Kbu1ticaUT2 的用户,并将其名称更改为“James”。使用 iOS SDK 我可以做到:

let ref = Database.database().reference()
let id = 5p3XT8TVFbVYNmFt0Kbu1ticaUT2
let databaseQuery = ref.child("users").queryOrdered(byChild: "uid").queryEqual(toValue: id)

为了查询数据库,它会过滤掉除我想要更改的用户之外的所有用户。但是这个查询现在将指向一个过滤的用户列表,它只包含我想要更新的用户,而不是用户本身。所以如果我尝试

databaseQuery.ref.updateChildValues(...)

数据库将如下所示:

我无法从 api 参考中了解如何直接访问被查询的孩子。我该怎么做?

【问题讨论】:

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

您可以在值字典中设置要更改的值

func updateValues(){
    let ref = Database.database().reference()
let id = 5p3XT8TVFbVYNmFt0Kbu1ticaUT2

    let values = ["name": txf_Name.text ?? "", “email”: txf_email.text ?? "", “role”: txf_Role.text ?? ""]
    ref.child(id).updateChildValues(values, withCompletionBlock: { (error, ref) in
        if(error != nil){
            print(error?.localizedDescription ?? "")
            return
        }
        print("Values Updated")            

    })

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 2021-02-11
    • 2021-10-04
    相关资源
    最近更新 更多