从firebase数据库检索数据:代码没有进入完成块或错误块 [英] Retrieving data from firebase database: code didn't go into the completion block or an error block

查看:139
本文介绍了从firebase数据库检索数据:代码没有进入完成块或错误块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



< img src =https://i.stack.imgur.com/IUF5j.pngalt =在这里输入图片描述>



这里是代码

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

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

-
返回commentsRef;


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

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





$ b

告诉我在代码中缺少的是什么。

解决方案

  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获取您的数据。 / li>
  • 只有你的代码有问题,你写了[commentsRef child:@Hello]。使用直接urlreference获取数据。

    I am trying to retrieve data from database but my code didn't go into the completion block or an error block. I didn't want any auth authentication from user and save data in database from an app. Just want to retrieve data from database. My database structure is like that

    Here is the code

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

    Tell me what I am missing in that code.

    解决方案

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

    • Here is the code with you will get your data from Firebase.
    • Only problem with your code is you wrote "[commentsRef child:@"Hello"]". Use direct urlreference to fetch data.

    这篇关于从firebase数据库检索数据:代码没有进入完成块或错误块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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