gk103 gk103 - 1 month ago 12
Swift Question

How to extract child of node in data snapshot

My firebase set up is as such:

Parent_node:{
Type:{
1476663471800:{ //This is a timestamp = Int64(date.timeIntervalSince1970 * 1000.0)
uid: USERS_UID;
}
}
}


how would I access the users uid? I have tried the following code, but its not extracting the UID

self.databaseRef.child("Parent_node/\(Type)").queryLimitedToLast(5).observeEventType(.Value, withBlock: { (snapshot) in

print(snapshot)
if let userDict = snapshot.value as? [String:AnyObject]{
for each in userDict{
let uidExtraced = each
print(uidExtraced)
//("1476663471700", [uid: USERS_UID])

Answer

First of all use snapshot.value?.allValues to get values and than parse it...

  if snapshot.exists() {
     for value in (snapshot.value?.allValues)!{
          print(value) // you get [uid: USERS_UID] here

          // ... parse it to get USERS_UID
          print("user_id -- \(value["uid"])")
      }
  }

With this method, order of child might be different for more eventful thing you can use snapshot.child