如何获取Firebase数据库中密钥的值并将其传递给TextField? [英] How do you get the value of a key in Firebase Database and pass it to a TextField?

查看:93
本文介绍了如何获取Firebase数据库中密钥的值并将其传递给TextField?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何使用Objective C在我的JSON数据库中找到密钥的值并将其传递给文本字段。我想从我的数据库中的第3个密钥收集数据(当然不包括标题节点),但是关于如何进行的每个解释似乎都不起作用。遍历到正确位置的代码是:

I was wondering how I can find the value of a key in my JSON database using Objective C and pass it to a textfield. I want to collect data from the 3rd key in my database (Excluding the title node, of course) but every explanation on how to just doesn't seem to work. The code for traversing to the right location is:

FIRUser *user = [FIRAuth auth].currentUser;
NSString* username = _usernameEntry.text;
if (user)
{
    [[[[_ref child: @"Users"] child:user.uid] child:@"Username"]observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot)
    {
        User *username = [[User alloc] initWithUsername:snapshot.value[@"username"]];
    }
    withCancelBlock:^(NSError * _Nonnull error)
    {
        NSLog(@"%@", error.localizedDescription);
    }];

    _emailAddressEntry.text = user.email;
}

对我来说。我的目的是在视图控制器导航到时,用户名文本字段显示当前用户名。这样,用户可以看到它现在是什么并编辑它,如果有任何错误/不满意的话(与电子邮件文本字段的完成方式类似)。

for me. My intention is for the username text field to show the current username, when the view controller is navigated to. This is so that the user can see what it is now and edit it, if anything was misstyped/isn't to their satisfaction (Similarly to how it's done with the email text field).

Side Note,用户名和电子邮件的存储/修改如下:

Side Note, the username and email are stored/modified like this:

NSString *userID = [FIRAuth auth].currentUser.uid;
NSString* username = _usernameEntry.text;

[[[_ref child:@"Users"] child:userID] setValue:@{@"Username": username}];

[[FIRAuth auth].currentUser updateEmail:_emailAddressEntry.text
                             completion:^(NSError * _Nullable error)
 {
     if(error)
     {
         [_emailValidationCheck setTextColor:[UIColor redColor]];
         _emailValidationCheck.text = @"Email Address modification was unsuccessful";
     }
     else
     {
         [_emailValidationCheck setTextColor:[UIColor greenColor]];
         _emailValidationCheck.text = @"Email Address modification was successful";
     }
 }];
}

此外,我还有一个问题还有两件事。 1)为什么在此遍历中始终会覆盖userID密钥?我将其定义为在注册视图控制器中具有名称User_ID和uid的值,但是当人们在我的应用程序的配置文件页面上编辑他们的用户名时,它会被覆盖并变成关键字。在此之前,它只是变成了名字(没有价值)。

Additionally, I have a question about two additional things. 1) Why does the userID key always get overwritten in this traversal? I define it as having the name User_ID and the value of uid in the registration view controller, however when people edit their username on the profile page in my application, it gets overwritten and turned into just the key. Before this, it was being turned into just the name (No value).

推荐答案

FIRUser *user = [FIRAuth auth].currentUser;

if (user)
{
    [[[[_ref child: @"Users"] child:user.uid] child:@"Username"] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot)
    {
        if (snapshot.exists)
        {
            _usernameEntry.text = snapshot.value;
        }
    }];

    _emailAddressEntry.text = user.email;
}

我的问题是解决方案。

这篇关于如何获取Firebase数据库中密钥的值并将其传递给TextField?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆