【问题标题】:Unable to retrieve data from firebase无法从 Firebase 检索数据
【发布时间】:2016-09-23 10:23:29
【问题描述】:

我正在尝试从 firebase 检索数据并尝试了不同的解决方案,但快照值返回 null。数据由“childbyAutoID”创建,我想使用当前 uid 检索它。

以下是三种解决方案,我试过了

解决方案 1:

    NSString *ID = [FIRAuth auth].currentUser.uid;
    [[[self.databaseRef child:@"User-Subscription"] child:ID] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {

        NSDictionary *childData = snapshot.value;
        NSLog(@"%@",childData); //Returns null
        // ...
    } withCancelBlock:^(NSError * _Nonnull error) {
        NSLog(@"%@", error.localizedDescription);
    }];

解决方案 2:

    NSString *ID = [FIRAuth auth].currentUser.uid; //User ID
    [[[self.databaseRef child:@"User-Subscription"] childByAutoId] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {

        NSDictionary *childData = snapshot.value;
        NSLog(@"%@",childData); //Return null
        // ...
    } withCancelBlock:^(NSError * _Nonnull error) {
        NSLog(@"%@", error.localizedDescription);
    }];

解决方案 3:

    NSString *ID = [FIRAuth auth].currentUser.uid; //User ID 
    [[[[self.databaseRef child:@"User-Subscription"] childByAutoId] queryEqualToValue:ID]observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {

        NSDictionary *childData = snapshot.value;
        NSLog(@"%@",childData); //Return null
        // ...
    } withCancelBlock:^(NSError * _Nonnull error) {
        NSLog(@"%@", error.localizedDescription);
    }];

非常感谢您指出错误的任何帮助。谢谢。

【问题讨论】:

    标签: objective-c xcode firebase firebase-realtime-database


    【解决方案1】:

    我找到了一个返回数据的解决方案。

    [[self.databaseRef child:@"User-Subscription"] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
    
        NSDictionary *childData = snapshot.value;
        NSLog(@"%@",childData); 
        // ...
    } withCancelBlock:^(NSError * _Nonnull error) {
        NSLog(@"%@", error.localizedDescription);
    }];
    

    但现在的问题是,如果 childbyAutoID 生成了 2,3 个用户订阅,我如何使用“subs_ID”查询它?

    【讨论】:

    • 我遇到了同样的问题。我尝试了这个解决方案,但没有得到结果。您对 databaseRef 初始化有什么作用吗?我想不出我的版本没有收到帖子的任何其他原因。
    • 我没有对 databaseRef 初始化做任何事情,但您还需要身份验证。因为,我正在使用带有 firebase 的 FB 登录,因此它会自动进行身份验证。您还需要检查您孩子的名字是否拼写正确。
    猜你喜欢
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-25
    • 2017-07-22
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    相关资源
    最近更新 更多