【问题标题】:Retrieving data from firebase realtime database IOS Swift从firebase实时数据库IOS Swift中检索数据
【发布时间】:2023-04-03 05:28:01
【问题描述】:

我一直在尝试从 firebase 实时数据库中检索数据,但没有任何运气。

我有一个 4 文本字段,您可以在其中输入用户配置文件数据,然后使用 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)

    }

}

【问题讨论】:

标签: ios firebase swift3 firebase-realtime-database


【解决方案1】:

试试这个

_ref = [[FIRDatabase database] reference];

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

  NSLog("%@",snapshot.value)

} withCancelBlock:^(NSError * _Nonnull error) {

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

}];

--- 编辑 ----

在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}];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 2020-12-09
    • 2018-08-04
    • 2021-02-06
    • 2017-10-06
    • 2018-09-04
    • 2019-09-17
    相关资源
    最近更新 更多