Denis Semenikhin
Denis Semenikhin

Reputation: 19

updateChildValues() does not work, but setValue() does

I'm trying to implement fan out and here is the problem: When I'm trying to updateChildValues in my usersOrdersRef reference, it just does nothing. I expect it to create the user_orders node, and child node with my userid and put a value there.

If I use setValues instead, it works fine, but I need to add values without deleting all previous data. What am I doing wrong?

Code:

for item in orderArray {
    let childRef = ref.child(item.date!)

    //this works fine
    childRef.updateChildValues(item.convertToDictionary() as! [AnyHashable : Any]) { [unowned self] (error, reference) in
        if error != nil {
            print(String(describing: error?.localizedDescription))
        }

        let usersOrdersRef = Database.database().reference().child("users-orders").child(self.appUser.uid)
        var primary = false
        if (item.precursor == "") { primary = true }

        //this does not work, but if i use "setValue" instead it will store data
        usersOrdersRef.updateChildValues([item.date : primary])
    }
}

Upvotes: 1

Views: 462

Answers (4)

Oliver Nguyen
Oliver Nguyen

Reputation: 119

I have the same problem

If I use

let newHashTagRef = Api.HashTag.REF_HASHTAG.child(word.lowercased()) newHashTagRef.updateChildValues([newPostId: true])

=> not working

when I replace

let newHashTagRef = Api.HashTag.REF_HASHTAG.child(word.lowercased()) newHashTagRef.setValues([newPostId: true])

=> it works but it will delete all previous data

Solution

let newHashTagRef = Api.HashTag.REF_HASHTAG.child(word.lowercased()).child(newPostId) newHashTagRef.setValue(true)

Upvotes: 1

MilanPanchal
MilanPanchal

Reputation: 2953

You just need to cast your optional data key into the updateChildValues function.

Updated Code:

for item in orderArray {
    let childRef = ref.child(item.date!)

    //this works fine
    childRef.updateChildValues(item.convertToDictionary() as! [AnyHashable : Any]) { [unowned self] (error, reference) in
        if error != nil {
            print(String(describing: error?.localizedDescription))
        }

        let usersOrdersRef = Database.database().reference().child("users-orders").child(self.appUser.uid)
        var primary = false
        if (item.precursor == "") { primary = true }

        // this will work now, as we have explicity cast the key
        usersOrdersRef.updateChildValues([item.date! : primary])
    }
}

Upvotes: 0

Pete Silva
Pete Silva

Reputation: 33

Your problem:

//this does not work, but if i use "setValue" instead it will store data
    usersOrdersRef.updateChildValues([item.date : primary])

I found myself in the same situation.

The problem was the data type of the key. When I casted the key to a String it started to function.

Hope this helps someone in the same situation.

Upvotes: 1

Denis Semenikhin
Denis Semenikhin

Reputation: 19

I resolve it by myself , now it works like this

let usersOrdersRef = Database.database().reference().child("users-orders").child(self.appUser.uid).child(item.date!) var primary = false if (item.precursor == "") { primary = true } usersOrdersRef.setValue(primary)

In that case setValue does not delete all previous data, just adds new

Upvotes: 0

Related Questions