从Firebase数据库中检索数据

嗨,我试图从firebase数据库检索数据。 这是我的代码

- (FIRDatabaseReference *) referenceFromURL:(NSString *)databaseUrl{ commentsRef = [[FIRDatabase database] referenceFromURL:databaseUrl]; return commentsRef; } -(void)viewWillAppear:(BOOL)animated { [commentsRef observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot *snapshot) { NSDictionary * post = snapshot.value; }]; } 

该引用是由数据库成功构建的,但代码是以块的形式编写的

“NSDictionary * post = snapshot.value;”

没有执行。

如果您可以更新到Firebase 3.0,将会很酷。

如果你想看看MakinGIANST – 发布firebase获取更多关于如何查询和使用新API的信息。

这是Android的,但它在Kotlin所以看起来像Swift

你可以轻松地做到这一点。 如果你想使用这个获取所有信息,你可以这样做。 确保你已经公开你的数据库

 @property (strong, nonatomic) FIRDatabaseReference *ref; 

然后在接口上定义属性

  self.ref = [[FIRDatabase database] reference]; [self.ref observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) { NSDictionary *usersDict = snapshot.value; NSLog(@"Information : %@",usersDict); }]; 

如果你想使用这个获取特定的部分,你可以做到这一点

  [[self.ref child:@"results"] observeEventType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) { NSDictionary *usersDict = snapshot.value; NSLog(@"Info : %@",usersDict); }]; 

我从Firebase获取Json

 { "results" : [ { "name":"test 1", "URL" : "URL STRING" }, { "name":"test 1", "URL" : "URL STRING" }, { "name":"test 1", "URL" : "URL STRING" }, { "name":"test 1", "URL" : "URL STRING" } ] }