Firebase gets swift ios baby id

My Firebase looks like this. Bellow Active_Orders appears childs with different names depending on their UID (user ID).

And this is my code to get the identifier of the child regardless of the name. But this does not seem to work at all. What is the correct way to get child ID ? Thanks

 databaseRef.child("Active_Orders").observeEventType(FIRDataEventType.Value, withBlock: { snapshot in //Get customerUID let customerUID = snapshot.value!.objectForKey("username") as! String self.setText("customerUID", value: customerUID) 
+5
source share
2 answers

It is difficult to say from your question what exactly you are doing, but does this make you the list that you need?

 databaseRef.child("Active_Orders").observeEventOfType(.Value, withBlock: { (snapshot) in if let result = snapshot.children.allObjects as? [FIRDataSnapshot] { for child in result { var orderID = child.key as! String print(orderID) } } }) 

I believe that this block should iterate over all child elements inside "Active_Orders" and print out their key value (which is similar to what you are trying to print).

+11
source

For Swift 3 and Firebase 4

 let ref = Database.database().reference() ref.child("Active_orders").observe(.value, with: { snapshot in if !snapshot.exists() {return} //may want to add better error handling here. let info = snapshot.value as! NSDictionary print("info keys are:", info.allKeys) }) 
+1
source

All Articles