Retrieve childs name Firebase Database Swift

I'm looking to retrieve the names of all the child of a section in my Firebase Database. Then I want to put them in an array.

{
  "sVkyFjTI9yOTlOPFzUv5MvFozlh2" : {
    "groups" : {
      "group 1" : "9FA02017-172B-434B-A873-518854697CCC",
      "group 2" : "B52743E0-8441-40FB-98BA-51920F31EFE6",
      "group 3" : "1A320965-88A3-42FF-A917-0D93BE39B357"
    }
  }
}

I've done this already:

ref.child("user_profile").child(user!.uid).child("groups/").observeSingleEventOfType(.Value, withBlock: { snapshot in
        if !snapshot.exists() { return }
        let groupDict = snapshot.value! as? [string]
}) 

But I only need the child names in an array (here: 'group 1','group 2','group 3') and not the values of them. Thanks for the help.

Upvotes: 1

Views: 1792

Answers (1)

I used this code and it's work perfectly: var ref = Firebase(url: "https://.firebaseio.com/restaurants/")

ref.child("user_profile").child(user!.uid).child("groups").observeEventType(.Value, withBlock: {
        snapshot in
        var groupNames = [String]()
        for group in snapshot.children {
            groupNames.append(group.key)
        }
        print(groupNames)
    })

Upvotes: 5

Related Questions