简体   繁体   English

Swift:无法更新Firebase数据库值

[英]Swift: Unable to update a Firebase DB Value

I am trying to update the value of an entry in my Firebase DB from false to true when I press a button. 当我按下按钮时,我试图将Firebase数据库中条目的值从false更新为true。 I can get the value of the current snapshot with the following code: 我可以使用以下代码获取当前快照的值:

@IBAction func test(_ sender: Any) {

ref.child("Sell_Request").queryOrdered(byChild: "name").queryEqual(toValue: requestUsername).observeSingleEvent(of: .value, with: { (snapshot : FIRDataSnapshot) in

    print(snapshot)

    })
}

I am trying to use the following code to update the value of "Request_Made" from false to true, but am getting a SIGBRT. 我正在尝试使用以下代码将“ Request_Made”的值从false更新为true,但正在获取SIGBRT。

@IBAction func test(_ sender: Any) {


        ref.child("Sell_Request").queryOrdered(byChild: "name").queryEqual(toValue: requestUsername).setValue(true, forKey: "Request_Made");

    }

Here is how my DB entry looks in FireBase: 这是我的数据库条目在FireBase中的外观:

在此处输入图片说明

I believe you still need to observe the query. 我相信您仍然需要观察查询。

@IBAction func test(_ sender: Any) {

    ref.child("Sell_Request").queryOrdered(byChild: "name").queryEqual(toValue: requestUsername).observeSingleEvent(of: .value, with: { snapshot in 

       for child in snapshot.children {           

           if let item = child as? FIRDataSnapshot {

               item.ref.setValue(true, forKey: "Request_Made");
           }
       }
    }
}

This would be the correct way to do that. 这将是正确的方法。

ref.child("Sell_Request").queryOrdered(byChild: "name").queryEqual(toValue: requestUsername).observeSingleEvent(.value, with: { dataSnapshot in
    let enumerator = dataSnapshot.children
    while let sell_request = enumerator.nextObject() as? FIRDataSnapshot {
        sell_request.ref.child("Request_Made").setValue(true)
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM