Reputation: 641
I am trying to update the value of an entry in my Firebase DB from false to true when I press a button. 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.
@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:
Upvotes: 1
Views: 204
Reputation:
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)
}
})
Upvotes: 1
Reputation: 1038
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");
}
}
}
}
Upvotes: 1