问题
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"}
回答1:
So it was a dumb error but one that I think might help people in the future:
My node was titled "english" and [DataSource sharedInstance].languageSelected was "English"
Basically, the takeaway is that nodes in Firebase are case sensitive.
来源:https://stackoverflow.com/questions/34270940/the-children-property-on-firebase-ios-shows-up-as-nil-when-there-are-child-node