gk103 gk103 - 2 months ago 15
Swift Question

Firebase: how to extract child key value from child node swift

How do i extract the ID value of a child (i.e. followerID1, followerID2...)? The firebase is structured as follows:

Followers:{
USER_ID:{
followerID1 = false;
followerID2 = true;
followerID3 = true;
}
}


Im doing the following in xCode, but its showing me the entire node under USER_ID:

self.databaseRef.child("Followers").child(self.USER_ID).Value, withBlock: { (snapshot:FIRDataSnapshot) in
self.userFollowDump = snapshot
})


Thanks! I am new to firebase :)

Answer

Try this:-

self.databaseRef.child("Followers").child(self.USER_ID).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
     if let userDict = snapshot.value as? [String:AnyObject]{
          for each in userDict{
             let follwerID = each.0 as! String // Every follwers ID.
        }
    }
 })

But if you are looking for the followerId's whose value is true

   FIRDatabase.database().reference().child("Followers").child("self.USER_ID").queryOrderedByValue().queryEqualToValue(true).observeSingleOfEvent(.Value, withBlock: {(snap) in
         if let snapDict = snap.value as? [String:AnyObject]{
           for each in snapDict{

                print(each.key)
            }
        }
    })