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

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 help would be great appreciated

  • How to write to the last child of a Firebase ref in Swift?
  • Firebase Multiple observer duplicates cell
  • FIRDatabaseQuery: how to do an inner join
  • Firebase databases replication
  • Linker command failed with exit code 1 after installing Cocopods and firebase pod
  • How to clustered markers from Firebase in GoogleMaps for iOS
  • Here is the code to print log

    NSLog(@"snapshot = %@",snapshot.value);
    for (NSString *keyPath in snapshot.value) {
         NSLog(@"keyPath = %@",keyPath);
    }
    

    Here is the log

    2016-04-20 11:48:04.177 EarCrush[5241:79090] snapshot = {
        "-KFlrXzqsSa50GVnJkqk" =     {
            conversationID = "2886-2377";
            text = Okteh;
            time = "20-04-2016 11:12";
        };
        "-KFlrgrvdR_uj2o1ltaw" =     {
            conversationID = "2886-2377";
            text = "How are you determining";
            time = "20-04-2016 11:12";
        };
        "-KFlrhSBbVnx4XyWjQPy" =     {
            conversationID = "2886-2377";
            text = "Message 2 ..... ";
            time = "20-04-2016 11:12";
        };
        "-KFlrhzx0TK5CeSR3aAA" =     {
            conversationID = "2886-2377";
            text = "Message 3: Hello world ";
            time = "20-04-2016 11:12";
        };
    }
    2016-04-20 11:48:04.177 EarCrush[5241:79090] keyPath = -KFlrhzx0TK5CeSR3aAA
    2016-04-20 11:48:04.178 EarCrush[5241:79090] keyPath = -KFlrhSBbVnx4XyWjQPy
    2016-04-20 11:48:04.179 EarCrush[5241:79090] keyPath = -KFlrXzqsSa50GVnJkqk
    2016-04-20 11:48:04.179 EarCrush[5241:79090] keyPath = -KFlrgrvdR_uj2o1ltaw
    

    Firebase server image
    enter image description here

    Solutions Collect From Internet About “IOS Firebase: Loop all key in snapshot.value get messy position”

    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.