0

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 enter image description here

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??

HaVaNa7
  • 330
  • 1
  • 3
  • 14
  • Please publish a mcve: https://stackoverflow.com/help/mcve. – Maciej Jureczko Oct 05 '17 at 19:53
  • Your code works for me; if I have 5 users then 5 users are added as child nodes of the utenti node. If I run it again, it will in fact overwrite the existing user nodes because the user.id is the same 5 users. It's unclear what *overwrite previous users already stored* means and also unclear what the updateChildValues part of the code is supposed to be doing. Can you further clarify the question? – Jay Oct 07 '17 at 15:34

3 Answers3

3

This is what you're looking to do:

root.child("groups/\(groupId!)/utenti/\(user.id!)").setValue("true")
Torewin
  • 887
  • 8
  • 22
  • That would only overwrite the userID node inside utenti. You tested it? – Torewin Oct 05 '17 at 23:21
  • sure I tested id, it overwrite entire node utenti with new values only – HaVaNa7 Oct 05 '17 at 23:38
  • Revised. This is an example coming directly from google on how to update a child node without updating the entire field. Please make sure "true" is in quotes as Firebase cannot receive booleans. – Torewin Oct 06 '17 at 00:13
  • exactly same result... thnx anyway for the effort! – HaVaNa7 Oct 06 '17 at 00:18
  • Are you sure? Remember user.id is based on the user you are using. Log into a different user and you'll see another user field populate. – Torewin Oct 06 '17 at 00:27
  • I think I am confused on your issue - you posted that your updateChildValues overwrites previous pairs? This is how it's supposed to work - each key has to be unique. What is your end goal and I can tell you how to achieve it. – Torewin Oct 06 '17 at 15:31
  • my goal is to append new child of type (key:values) under "utenti node". if I do this: root.child("groups/\(groupId!)/utenti/\(user.id!)").setValue("true") Firebase create a new child under utenti like this: - utenti --4983456793124:true --4320820824556:true ---+asdfgfd860dgf67 : true I need this: - utenti --4983456793124:true --4320820824556:true --asdfgfd860dgf67: true – HaVaNa7 Oct 06 '17 at 15:43
  • look this to understand better: https://stackoverflow.com/questions/38999383/synchronized-array-for-likes-followers-best-practice-firebase-swift/39006089 – HaVaNa7 Oct 06 '17 at 16:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/156117/discussion-between-torewin-and-havana7). – Torewin Oct 06 '17 at 18:01
1

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
Jay
  • 34,438
  • 18
  • 52
  • 81
  • thnx but I don't want to add a child under utenti, it should be a way to add other sub child without overwrite... – HaVaNa7 Oct 07 '17 at 14:33
  • @HaVaNa7 That's what your question states: *add a new pair of key:values in the node utenti in my Firebase database*. Also, my code does NOT overwrite anything. It can't. Each time it is run, it adds a new child to the utenti node and does not overwrite. Run it 5 times and you will have 5 child nodes. – Jay Oct 07 '17 at 14:35
  • sorry @Jay but I'm quite new to firebase and I followed a tutorial that say to build the db in this way, your code works as expected but it overwrite existing db values – HaVaNa7 Oct 07 '17 at 14:41
  • @HaVaNa7 Your database is fine. Your question asks how to add a key: value pair under the utenti node, which is what my answer does. If that's not the question please update it so we understand what you are asking. If you want your keys to be number, number1, number2 then instead of .childByAutoId, use .child("number") and then .child("number1") etc. That will not overwrite anything. However, if you are writing an array to Firebase, that always overwrites the data, and array's should be avoided in general. If you include code in your question, we may be better able to assist. – Jay Oct 07 '17 at 14:46
0

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

HaVaNa7
  • 330
  • 1
  • 3
  • 14