几天前我问了这个关于从Firebase检索数据的问题。该问题的答案包括使用FEventTypeValue和快照上的.childs属性的for循环子节点。它运行得很好,做了我需要它做的事情。
然而,我尝试过使用类似的逻辑,它将.childs显示为nil。
数据如下:
--languagesList
----English
-------Ari
---------Age: 28
---------Country: United States
---------distance: 2
-------Philip
---------Age: 27
---------Country: United States
---------distance: 1
----Spanish
-------Mauricio
---------Age: 30
---------Country: Mexico
---------distance: 4
以下是代码示例(在viewDidLoad中):
NSString* selectedLanguagePath = [NSString stringWithFormat:@"languagesList/%@", [DataSource sharedInstance].languageSelected];
Firebase *languagesRef = [[DataSource sharedInstance].ref childByAppendingPath:selectedLanguagePath];
[[languagesRef queryOrderedByChild:@"distance"] observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
for ( FDataSnapshot *child in snapshot.children) {
NSDictionary *dict = child.value;
NSString *uid = child.key;
NSLog(@"%@", child.key);
NSLog(@"%@", child.value);
NSLog(@"%@", snapshot.key);
NSLog(@"%@", snapshot.value);
[self.distanceMutableArray addObject:dict];
}
NSLog(@"%@", snapshot.key);
NSLog(@"%@", snapshot.value);
NSLog(@"%@", snapshot.children);
NSLog(@"%lu", (unsigned long)snapshot.childrenCount);
NSLog(@"%@", snapshot.priority);
NSLog(@"%@", snapshot.ref);
NSLog(@"%@", self.distanceMutableArray);
}];
在本例中,for循环从不运行,因为.childs为零。snapshot.key是"English",在这种情况下这是正确的。Snapshot.childrenCount是零,但它不应该是。
有什么建议吗?我想做的是用代码的"English"节点中按距离排序的所有信息填充一个数组。因此,它将是一系列看起来像这样的字典:
NSArray* englishArray = @[@{"Age": @27, "Country": @"United States", "distance": @1}, @{"Age": @28, "Country": @"United States", "distance": @2}
};
还有一件事,在我的安全和规则中,我有:
"languagesList": {".read": true, ".write": true, ".indexOn" : "distance"}
所以这是一个愚蠢的错误,但我认为这可能会在未来帮助人们:
我的节点名为"english"和[DatasourcesharedInstance]。所选语言为"English"
基本上,Firebase中的节点是区分大小写的。