Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SetValue in Swift from Firebase removes old value's

In the documentation it says that I should use this code to set new value's:

self.ref.child("users").child(user.uid).setValue(["username": username])

This way, I can set an username. However, if I want to add another value later on, like XP points, I use the following line of code:

var XPPoints = 1    
self.ref.child("users").child(user.uid).setValue(["XPPoints": XPPoints])

This, however, deletes the username value, or any other value that was stored before. It replaces all value's. I tried also the second thing the docs are saying at: https://firebase.google.com/docs/database/ios/read-and-write#basic_write which is this line of code:

self.ref.child("users/(user.uid)/username").setValue(username)

This will crash my app instantly when starting up. My question is: How can I add data, and not replacing the old value's?

Edit: Fixed it by changing the code to: self.ref.child("users/\(user.uid)/username").setValue(username)

like image 418
Petravd1994 Avatar asked Dec 24 '22 21:12

Petravd1994


1 Answers

Your solution shouldn't fix the problem you are having. You need to use updateChildValues instead of setValue. This adds the values instead of overwriting everything in the referenced node.

like image 76
Jordi Bruin Avatar answered Dec 28 '22 10:12

Jordi Bruin