Ralph simmonds
Ralph simmonds

Reputation: 55

How to add data to to an already existing reference in the firebase database?

I am constructing an iOS app, and once the user logs in, they each get their own individual reference to keep track of their email they signed in with. However after they sign in and it segues to the next view controller where the user selects an amount to store in the database, I am creating a new reference and I don't want to do to this. I just want to add on to the reference they were given when they logged in. How can I modify my code so that each following data reference they store while they are logged in becomes a child of the original reference created that's associated with that specific user?

This is what my database looks like, I create the amount reference of the controller after the login controller where the email reference is created, and I just want to append the amount referred to the already existing one that holds their emailenter image description here

Upvotes: 1

Views: 722

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598740

I can think of two options.

The first one is to write a level deeper:

var ref: DatabaseReference!

ref = Database.database().reference()

self.ref.child("users").child(user.uid).child("Amount").setValue(1)

The other approach is to update the specific field:

var ref: DatabaseReference!

ref = Database.database().reference()

self.ref.child("users").child(user.uid). updateChildValues(["Amount": 1])

Upvotes: 2

Related Questions