How to update data without existing ones getting affected in Swift and Firebase?

Zahraa Source

so in my app, users input data into the text field which has been saved and stored in my firebase database. But when the input is changed, it completely overrides what I had in the database before.

Here's the code;

    @IBAction func cont(_ sender: UIButton) {
    // Database reference
    ref = Database.database().reference()

    //Referencing the actual table the data is updated and stored in
    let customerRef = ref.child("customerDetails")
    customerRef.updateChildValues(["email": emailTextField.text!])
    customerRef.updateChildValues(["firstName" : firstNTextField.text!])


Here's the image of my database;

enter image description here

There will be multiple data collected and I will be adding more fields such as last name and stuff into. So how can existing data not get affected?



answered 2 weeks ago Daniel Dramond #1

Using this will allow you to add lists to your database, inside of customerDetails, without affecting any existing data. Also, you can just update your values in one line like so:

let ref = Database.database().reference()
let customerRef = ref.child("customerDetails").childByAutoId()
customerRef.updateChildValues(["email": emailTextField.text!,
                               "firstName": firstNTextField.text!])

comments powered by Disqus