2016-09-23 117 views
1

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

Picture from Firebase

以下三种解决方法,我试图

解决方案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); 
    }]; 

任何帮助指出错误是高度赞赏。谢谢。

回答

0

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

[[self.databaseRef child:@"User-Subscription"] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) { 

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

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

+0

我遇到同样的问题。我试过这个解决方案,我没有得到结果。有没有什么你与databaseRef初始化做什么?我无法弄清楚我的版本没有收到帖子的任何其他原因。 –

+0

我没有做任何与databaseRef初始化,但你也需要认证。因为我使用FB登录与Firebase,因此它是自动验证。你也需要检查你的孩子的名字是否拼写正确。 – WasimSafdar