Reputation: 330
EDITED
i have to add a new pair of key:values in the node utenti in my Firebase database structured as below, but without deleting previous pairs already stored in db
I get those pair from an array of custom objects where I have stored all user I have to add
so with for cycle:
for user in self.usersarray {
let utentiRef = root.child("groups").child(groupId!).child("utenti")
let refToAdd = utentiRef.child(user.id!)
refToAdd.setValue(true)
}
it works and add all the users in the array but it overwrite previous users already stored on firebase.
I noticed that when from another view controller i run
root.child("groups").child(groupId!).child("utenti").updateChildValues([user.id!: true])
the single user is added without overwriting, so where is the problem??
Upvotes: 0
Views: 1952
Reputation: 330
I found that I was calling another update some line above with a set value for the entire node groupId and so it was erasing and then repopulate only with the new values, sorry to wasted your time... Anyway both answers does the same of initial code, no errors, only different ways to do same thing
Upvotes: 0
Reputation: 35659
This should be straight forward. I think you're missing the piece that adds a child node to the utenti node. Try this:
let utentiRef = self.ref.child(groups).child(groupId).child("utenti")
let refToAdd = utentiRef.childByAutoId()//or whatever the key name is; .child("xyz")
refToAdd.setValue(true)
results in
groups
the_group_id
utenti
-k99asd9j9jaasj: true
and the existing nodes are not overwritten. If the code is run again, it will result in
groups
the_group_id
utenti
-k99asd9j9jaasj: true
-kYU9isj99f0392: true
Upvotes: 1
Reputation: 907
This is what you're looking to do:
root.child("groups/\(groupId!)/utenti/\(user.id!)").setValue("true")
Upvotes: 3