I asked this question a few days ago about retrieving data from Firebase. The answer to the question included a for-loop through child nodes using FEventTypeValue and the .children property on snapshots. It worked perfectly, doing what I needed it to do.
However, I've tried using similar logic, and it shows the .children as nil.
Here's what the data looks like:
--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
Here's the code example (it's in 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);
}];
In the example, the for loop never runs because .children is nil. snapshot.key is "English", which is correct in this case. Snapshot.childrenCount is zero though, which it shouldn't be.
Any advice? What I'm trying to do is populate an array with all of the information in the "English" node of the code that is sorted by distance. So it would be an array of dictionaries that looked like this:
NSArray* englishArray = @[@{"Age": @27, "Country": @"United States", "distance": @1}, @{"Age": @28, "Country": @"United States", "distance": @2}
};
One more thing, in my Security and rules, I have:
"languagesList": {".read": true, ".write": true, ".indexOn" : "distance"}