crispy2k12
crispy2k12

Reputation: 355

Adding data to a specific UID in firebase

i've been trying to store user inputted data to the their personal uid in firebase but so far i've only been able to do this when the user created an account and what im trying to find out is how to store data from a text field in xcode to the firebase realtime database under a unique uid, that the user gets when they create an account??

for example: i have 3 textfields that will store the user information to the already assigned UID when they created the account at the start of the application.

let uid = user.uid
ref.child("users").child(user!.uid).setValue(["DOB": self.DOB. text!,
"Address": self.address.text!, "age":self.age.text!])

Upvotes: 2

Views: 3446

Answers (1)

crispy2k12
crispy2k12

Reputation: 355

I've managed to solve the problem, assuming the user is already authenticated within the application: create an IBoutlet for the textfield named "yourtextfieldname" convert the textfield data into an NSSTRING and place the otherstr variable in the setValue parameter. then add the function to the button action.

   let userID = FIRAuth.auth()?.currentUser?.uid
   //gets current user uid

   func test() {

    let str = yourtextfieldname.text
    let otherstr = str! as NSString
   //convert textfield datatype NSString

    self.ref.child("users").child(userID!).child("yourtextfieldname").setValue(otherstr)

}

@IBAction func but(sender: AnyObject) {
    //calling test function
  updateProfile()


}

Json data structure

{
 "users": {
     "unique UID": {
         "test": "strTest"

               }
            }
 }

Upvotes: 3

Related Questions