IOS Firebase: Loop all key in snapshot.value get messy position

前端 未结 1 555
挽巷
挽巷 2021-01-26 05:16

I receive the snapshot from Firebase server like the log below. Now I want to loop all key in snapshot but I got messy position.

I don\'t know why this happened. Any h

相关标签:
1条回答
  • 2021-01-26 06:11

    When you print a FDataSnapshot, it prints the JSON structure. Since JSON has no defined order for child nodes, the order in which they are shown is undetermined (although frequently it will print the children in lexicographical order of they key as you get here).

    Firebase's queries can be used to get the data in a specific order. For example from this question:

    [[languagesRef queryOrderedByChild:@"distance"] observeEventType:FEventTypeValue withBlock:^(FDataSnapshot *snapshot) {
    
        for ( FDataSnapshot *child in snapshot.children) {
    
            NSLog(@"child.key = %@",child.key);
    
        }
    

    The Firebase snapshot contains additional information about the ordering of the child data, that it doesn't show when you print that snapshot. When you loop over the children of the snapshot, this ordering information is used to ensure the correct order in the loop above.

    0 讨论(0)
提交回复
热议问题