简体   繁体   English

应用程序未从Firebase检索数据

[英]App not retrieving data from firebase

I cant seem to retrieve any information from my firebase database. 我似乎无法从我的Firebase数据库检索任何信息。 I created a table with both author and title available. 我创建了一个表,其中提供了作者和标题。

数据库截图

This is the code i ran in my ios app, but the app just keeps crashing. 这是我在ios应用程序中运行的代码,但该应用程序一直崩溃。

// Get a reference to our posts
Firebase *ref = [[Firebase alloc] initWithUrl: @"**********"];

[ref observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
    NSLog(@"%@", snapshot.value[@"author"]);
    NSLog(@"%@", snapshot.value[@"title"]);
}];

}

I cant tell whats going wrong, any help with this i will be appreciated. 我无法告诉您出了什么问题,对此我的任何帮助将不胜感激。

Thank you 谢谢

change event type to 将事件类型更改为

[ref observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
     NSLog(@"%@", snapshot.value[@"author"]);
      NSLog(@"%@", snapshot.value[@"title"]);
 }];

You would generally not store data like that right under your root node - it would generally be a child node(s) stored under the root node, like this: 通常,您通常不会在根节点下存储这样的数据-通常,它是在根节点下存储的子节点,如下所示:

sizzling-inferno-255
  book_node_0
    author: test_author
    title: test title
  book_node_1
    author: another author
    title: another title title


Firebase *ref = [[Firebase alloc] initWithUrl: @"sizzling-inferno-255"];

[ref observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
    NSLog(@"%@", snapshot.value[@"author"]);
    NSLog(@"%@", snapshot.value[@"title"]);
}];

will iterate over each book node and return a snapshot of that node. 将遍历每个书节点并返回该节点的快照。 So the first time a snapshot is returned, it will be 因此,第一次返回快照时,它将是

  book_node_0
    author: test_author
    title: test title

and the second time a snapshot is returned will be 而第二次返回快照将是

  book_node_1
    author: another author
    title: another title title

the book_node keys are generated with childByAutoId. book_node键是通过childByAutoId生成的。

ChildAdded reads in each child node one at a time as a snapshot. ChildAdded一次读取每个子节点一个快照。

Value reads in everything in the node; 值读入节点中的所有内容。 all child nodes, those children etc and can be a large amount of data. 所有子节点,那些子节点等等,并且可能是大量数据。 If the ref node contains children, like in this answer, all of the nodes are returned and they need to be iterated over to get the child data, so within the observe block... 如果ref节点包含子节点(如此答案中所示),则将返回所有节点,并且需要对其进行迭代以获取子节点数据,因此在observe块中...

for child in snapshot.children {
   NSLog(@"%@", child.value[@"author"]);
}

Edit - and to specifically answer the OP question, here's how you do it with the Firebase structure in the question: 编辑-并专门回答OP问题,这是您使用问题中的Firebase结构的方法:

self.myRootRef = [[Firebase alloc] initWithUrl:@"https://**********"];
[ref observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) { 
    NSString *key = snapshot.key;
    NSString *value = snapshot.value;

    if ( [key isEqualToString:@"author"] ) {
        NSLog(@"author = %@", value);
    } else if ([snapshot.key isEqualToString:@"title"] ) {
        NSLog(@"title = %@", value);
    }
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM