2017-02-20 108 views
0

我一直在嘗試一段時間從Firebase實時數據庫檢索數據,但沒有任何運氣。從firebase實時數據庫檢索數據IOS Swift

我有一個4文本框,您可以在其中鍵入userprofile數據,然後使用user.uid將它存儲在firebase實時數據庫中,但我不知道如何將信息恢復回標籤,例如。

以下是我如何存儲數據的代碼。關於如何檢索數據的任何想法?謝謝你的幫助。

@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) 

    } 

} 
+0

檢查這 - 保存數據:http://stackoverflow.com/問題/ 37759614/firebase-retrieval-data-in-swift –

回答

0

試試這個

_ref = [[FIRDatabase database] reference]; 

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

    NSLog("%@",snapshot.value) 

} withCancelBlock:^(NSError * _Nonnull error) { 

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

}]; 

---編輯----

在火力

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}]; 
+0

不工作,我做錯了什麼? :) –