gk103 gk103 - 1 year ago 84
Swift Question

How to extract child of node in data snapshot

My firebase set up is as such:

1476663471800:{ //This is a timestamp = Int64(date.timeIntervalSince1970 * 1000.0)

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

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

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download