【问题标题】:Retrieving data from firebase database: code didn't go into the completion block or an error block从 firebase 数据库中检索数据:代码没有进入完成块或错误块
【发布时间】:2016-06-03 10:07:35
【问题描述】:

我正在尝试从数据库中检索数据,但我的代码没有进入完成块或错误块。我不希望用户进行任何身份验证并将数据从应用程序保存在数据库中。只想从数据库中检索数据。我的数据库结构是这样的

这里是代码

- (void)viewDidLoad {
    [FIRDatabase database].persistenceEnabled = YES;
    [self referenceFromURL:@"https://project-8326407164650950213.firebaseio.com"];
}
- (FIRDatabaseReference *) referenceFromURL:(NSString *)databaseUrl{

    commentsRef = [[FIRDatabase database] referenceFromURL:databaseUrl];
    return commentsRef;
}


-(void)viewWillAppear:(BOOL)animated
{
    [[commentsRef child:@"Hello"]
     observeEventType:FIRDataEventTypeValue
     withBlock:^(FIRDataSnapshot *snapshot) {
         NSDictionary * post = snapshot.value;

     }withCancelBlock:^(NSError * error){
         NSLog(@"%@",error.description);
     }];
}

告诉我该代码中缺少什么。

【问题讨论】:

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


    【解决方案1】:
            NSString *strUrl = [NSString stringWithFormat:@"https://project-8326407164650950213.firebaseio.com"];
        FIRDatabaseReference *ref = [[FIRDatabase database] referenceFromURL:strUrl];
        [ref observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
            NSDictionary *post = snapshot.value;
            NSLog(@"%@",post);
        }];
    
    • 这是您从 Firebase 获取数据的代码。
    • 您的代码唯一的问题是您编写了“[cmetsRef child:@"Hello"]”。使用直接 urlreference 来获取数据。

    【讨论】:

    • 我这里没有使用错误块。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多