0

I have been trying for some time now to retrieve data from firebase realtime database, but without any luck.

I have a 4 textfield where you can type in userprofile data, and then I store it in the firebase realtime database, using the user.uid, but I do not know how to retrieve the information back to a label for example.

Below is my code for how I store the data. Any ideas on how to retrieve the data? Thanks for any help.

@IBAction func saveAction(_ sender: Any) {

    if favTeamTextfield != nil && usernameTextfield != nil && fullNameTextfield != nil && phoneNumberTextfield != nil
    {
        //Post data to firebase
        ref?.child("Users").child((user?.uid)!).setValue(["Full Name": fullNameTextfield.text, "Username": usernameTextfield.text, "Phone Number": phoneNumberTextfield.text, "Favorite Soccer Team": favTeamTextfield.text]) 

        // dismiss the page
        presentingViewController?.dismiss(animated: true, completion: nil)     
    }
    else
    {
        let alertController = UIAlertController(title: "Oops!", message: "Please enter a username to save!", preferredStyle: .alert)

        let defaultAction = UIAlertAction(title: "OK", style: .cancel, handler: nil)

        alertController.addAction(defaultAction)

        self.present(alertController, animated: true, completion: nil)

    }

}
Arasuvel
  • 2,971
  • 1
  • 25
  • 40

1 Answers1

0

try this

_ref = [[FIRDatabase database] reference];

[[_ref child:@"userDetail"] observeEventType:FIRDataEventTypeChildAdded withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {

  NSLog("%@",snapshot.value)

} withCancelBlock:^(NSError * _Nonnull error) {

    NSLog(@"%@",[error localizedDescription]);

}];

--- Edit ----

Saving data in firebase

 FIRUser *user = [FIRAuth auth].currentUser;
 FIRDatabaseReference *ref = [[FIRDatabase database] reference];
 [ref keepSynced:YES];
 [[[ref child:@"userDetail"] childByAutoId]   setValue:@{@"user_id":user.uid,@"user_name":user.displayName,@"user_email":user.email}];
Harshal Valanda
  • 5,331
  • 26
  • 63