user2411290
user2411290

Reputation: 641

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. 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:

enter image description here

Upvotes: 1

Views: 204

Answers (2)

user3476154
user3476154

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

dst3p
dst3p

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

Related Questions