crispy2k12
crispy2k12

Reputation: 355

Retrieve data stored by childByAutoID in firebase

In firebase i'm trying to retrieve data stored in the firebase realtime database by using the childByAutoID() to give the data a unique identifier but the problem is i'm not entirely sure how to achieve this?

func retData(){
    rootRef.child("users").child(userID).ChildByAutoID().observeEventType(.Value){
        (snap: FIRDataSnapshot) in

    self.simpleLabel.text = snap.value?.description

    }   
 }

Json data structure

-userID
   -childByAutoID
      -player1
         -email 

Upvotes: 2

Views: 965

Answers (1)

Dravidian
Dravidian

Reputation: 9955

If your JSON structure is:-

-users
  -childByAutoID
    -playerID
      -email

Use :-

 rootRef.child("users").observeEventType(.Value, withBlock: {(snap) in

           if let userDict = snap.value as? [String:AnyObject]{

               for each in userDict as [String: AnyObject] {

                   let autoID = each.0
                   //Here you retrieve your autoID

                   rootRef.child("users").child(autoID).child("player1").observeEventType(.Value, withBlock: {(playersDict) in
                      if let playerDictionary = playerDict.value as? [String:AnyObject]{
                        let emailID = playerDictionary["email"] as! String

                          //print(emailID)

                     }
             })
      }
   }
})

Upvotes: 1

Related Questions