无法从firebase检索数据

时间:2016-09-23 10:23:29

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

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

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

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

1 个答案:

答案 0 :(得分: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);
}];

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

相关问题