2
votes

I have this code in my app:

DataService.dataService.USERS_REF.childByAppendingPath(DataService.dataService.CURRENT_USER_ID).childByAppendingPath("groups").observeSingleEventOfType(.Value, withBlock: { snapshot in

  print(snapshot)

})

If I print the result, I only get a snapshot with this value:

Snap (groups) {
    "-KBAX-d4UNQMZErrTqPD" = true;
    "-KBAX0rrrR-trRZc408F" = true;
    "-KBAX2-lrGd-Bx2zVizv" = true;
    "-KBCL_xUjm-kf1JsGgpA" = true;
    "-KBCOlullSmzi46ecf9J" = true;
}

If I print snapshot.key I get

groups

and if I print snapshot.value I get

{
    "-KBAX-d4UNQMZErrTqPD" = true;
    "-KBAX0rrrR-trRZc408F" = true;
    "-KBAX2-lrGd-Bx2zVizv" = true;
    "-KBCL_xUjm-kf1JsGgpA" = true;
    "-KBCOlullSmzi46ecf9J" = true;
}

Is there a way to iterate through all the snapshot values and get all the keys and values of each one? In this case, there are 5 keys and values in this snap.

1

1 Answers

2
votes

Nevermind, I already found the solution. In this case:

DataService.dataService.USERS_REF.childByAppendingPath(DataService.dataService.CURRENT_USER_ID).childByAppendingPath("groups").observeSingleEventOfType(.Value, withBlock: { snapshot in

    for rest in snapshot.children.allObjects as! [FDataSnapshot] {
        print(rest.key)
    }
})