Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating a single value in a dictionary in firebase - Swift project

I'm working on iOS application using swift and firebase. I'm trying to let the user change his email and password and the rest of his information stored in firebase in the same view controller using separate buttons.

Here's example of one user from the dashboard:

    {
     "Users" : {
       "08e5443c-cdde-4fda-8733-8c4fce75dd34" : {
         "BusinessName" : "looliCake",
         "Category" : "Cooking",
         "City" : "Riyadh",
         "ContactMe" : "05551233210",
         "Details" : "many flaiver choose",
         "Email" : "[email protected]",
         "PhoneNumber" : "05551233210",
         "Provider" : "123",
         "ShortDescription" : "best pop cake ever . ",
         "Website1" : "www.looliCack.com",
         "Website2" : "https://www.instagram.com/loolicack/"
        }
      }
    }

Here's the code:

let mainpass = NSUserDefaults.standardUserDefaults().objectForKey("password") as? String
let mainEmail = NSUserDefaults.standardUserDefaults().objectForKey("Email") as? String



@IBAction func EditEmail(sender: AnyObject) {

    if EmailAddressTF.text==""
    {
        let alert = UIAlertController(title: "Oops!", message:"Make sure to fill in all required textfields", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
        self.presentViewController(alert, animated: true){}
    }
    else
    {
        let isEqu = (mainEmail == self.EmailAddressTF.text)
        if(isEqu == true) {

        } else {
            let ref = Firebase(url: "https://businesswallet.firebaseio.com/")

            ref.changeEmailForUser(mainEmail, password: mainpass, toNewEmail: self.EmailAddressTF.text, withCompletionBlock: { error in

                if error != nil {

                    if let errorCode = FAuthenticationError(rawValue: error.code) {

                        switch (errorCode) {
                        case .EmailTaken:
                            print("Email taken")
                            let alert = UIAlertController(title: "Oops!", message:"Sorry,Email taken", preferredStyle: .Alert)
                            alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
                            self.presentViewController(alert, animated: true){}

                        case .InvalidEmail:
                            print("invalid email")
                            let alert = UIAlertController(title: "Oops!", message:"invalid email", preferredStyle: .Alert)
                            alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
                            self.presentViewController(alert, animated: true){}


                        case .NetworkError:
                            print("Network Error")
                            let alert = UIAlertController(title: "Oops!", message:"Network error, check your connection", preferredStyle: .Alert)
                            alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
                            self.presentViewController(alert, animated: true){}

                        default:
                            print("Unknown Error")
                            let alert = UIAlertController(title: "Oops!", message:"Unknown Error", preferredStyle: .Alert)
                            alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
                            self.presentViewController(alert, animated: true){}
                        }
                    }





                } else {

                    NSUserDefaults.standardUserDefaults().setValue(self.EmailAddressTF.text, forKey: "Email")
                    print("Email changed successfully")

                    let newUser = [
                        "Provider": self.PasswordTF.text!,
                        "Email": self.EmailAddressTF.text,
                        "BusinessName": self.BusinessNameTF.text ,
                        "ShortDescription" : self.ShortDescriptionTF.text,
                        "Category" : self.itemSelected,
                        "City" : self.CityTF.text,
                        "ContactMe" : self.ContactMeTF.text,
                        "PhoneNumber" : self.PhoneNumberTF.text,
                        "Website1": self.Website1TF.text,
                        "Website2": self.Website2TF.text,
                        "Details": self.DetailsTV.text
                    ]


                    self.ref.childByAppendingPath("Users").childByAppendingPath(self.ref.authData.uid).updateChildValues(newUser)
                    self.navigationController?.popViewControllerAnimated(true)

                }

            })

        } //Big Else


    }



} //EditEmailButton




@IBAction func EditPassword(sender: AnyObject) {

    if  PasswordTF.text==""
    {
        let alert = UIAlertController(title: "Oops!", message:"Make sure to fill in all required textfields", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
        self.presentViewController(alert, animated: true){}
    }
    else
    {
        if RepasswordTF.text != PasswordTF.text
        {
            let alert = UIAlertController(title: "Oops!", message:"You Entered Different Passwords", preferredStyle: .Alert)
            alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
            self.presentViewController(alert, animated: true){}
        }
        else {


            ref.changePasswordForUser( NSUserDefaults.standardUserDefaults().objectForKey("Email") as? String, fromOld: NSUserDefaults.standardUserDefaults().objectForKey("password") as? String, toNew: self.PasswordTF.text)
            {

                (ErrorType) -> Void in
                if ErrorType != nil {
                    print(ErrorType)
                    print("There was an error processing the request")
                }
                else
                {

                    NSUserDefaults.standardUserDefaults().setValue(self.PasswordTF.text, forKey: "password")
                    print("Password changed successfully")

                }

            }


            let newUser = [
                "Provider": self.PasswordTF.text!,
                "Email": self.EmailAddressTF.text,
                "BusinessName": self.BusinessNameTF.text ,
                "ShortDescription" : self.ShortDescriptionTF.text,
                "Category" : self.itemSelected,
                "City" : self.CityTF.text,
                "ContactMe" : self.ContactMeTF.text,
                "PhoneNumber" : self.PhoneNumberTF.text,
                "Website1": self.Website1TF.text,
                "Website2": self.Website2TF.text,
                "Details": self.DetailsTV.text
            ]


            self.ref.childByAppendingPath("Users").childByAppendingPath(self.ref.authData.uid).updateChildValues(newUser)
            self.navigationController?.popViewControllerAnimated(true)

        }


    } //else


}



@IBAction func EditBusinessInfo(sender: AnyObject) {

    if BusinessNameTF.text==""
    {
        let alert = UIAlertController(title: "Oops!", message:"Make sure to fill in all required textfields", preferredStyle: .Alert)
        alert.addAction(UIAlertAction(title: "OK", style: .Default) { _ in })
        self.presentViewController(alert, animated: true){}
    }
    else
    {

        let newUser = [
            "Provider": self.PasswordTF.text!,
            "Email": self.EmailAddressTF.text,
            "BusinessName": self.BusinessNameTF.text ,
            "ShortDescription" : self.ShortDescriptionTF.text,
            "Category" : self.itemSelected,
            "City" : self.CityTF.text,
            "ContactMe" : self.ContactMeTF.text,
            "PhoneNumber" : self.PhoneNumberTF.text,
            "Website1": self.Website1TF.text,
            "Website2": self.Website2TF.text,
            "Details": self.DetailsTV.text
        ]


        self.ref.childByAppendingPath("Users").childByAppendingPath(self.ref.authData.uid).updateChildValues(newUser)
        self.navigationController?.popViewControllerAnimated(true)

    }
}

In each button, I'm updating the entire dictionary in firebase! Which's wrong!

How can I update in firebase only the email value in email button, and provider in password button?

like image 553
Mariah Avatar asked Apr 30 '16 14:04

Mariah


1 Answers

This question and answer in the comments helped me out. Since there isn't an official answered question, I will add one with updated code cause the one in the comments is deprecated.

ref.child("yourKey").child("yourKey").updateChildValues(["yourKey": yourValue])

This worked perfectly for me.

like image 134
Ro4ch Avatar answered Nov 02 '22 23:11

Ro4ch