0

I have a Firebase Realtime Database and want to get the name of a child in the last part of a branch.

The database structure looks like this:

Database structure

How do I get the "FD4936E20732A42E" and possible other child names of "friends"?

"FD4936E20732A42E" and other possible childs of "friends" are not string values I know before runtime.

I know how I would for example access the value for a specific part of the database. But in this case using snapshot.value and snapshot.key did not work.

I really appreciate any kind of help. I am really desperate.

Update - I tried this but the output is a FIRDataSnapshot and I need a String. A downcast is not working:

let ref = Database.database().reference().child("visitors").child("107C081B-4117-4E2F-8E39-BF896D366B30")
ref.child("friends").observeSingleEvent(of: .value) { snapshot in
    for child in snapshot.children {
        print(child)
    }
}

The output I get:

Snap (FD4936E20732A42E) 1
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Once you have a `DataSnapshot` of `friends`, you can loop over its `children` as shown [here](https://firebase.google.com/docs/database/ios/lists-of-data#listen_for_value_events). I also recommend some of these search results: https://stackoverflow.com/search?tab=votes&q=%5bfirebase-realtime-database%5d%5bswift%5d%20loop%20over%20children. If you can't get that to work for your case after reading these, post a [minimal repro](http://stackoverflow.com/help/mcve) of where you got stuck. – Frank van Puffelen Nov 12 '22 at 17:52
  • Thank you for your help. I tried the snapshot.children approach but did not get the desired output. – Carlo Maryska Nov 16 '22 at 20:19
  • The code seems fine at first glance, and the output matches what I'd expect from it. Can you clarify what the desired output is? --- If you want to show the key(s) only, that'd be `print(child.key)` as also shown here: https://stackoverflow.com/a/39229537/209103 – Frank van Puffelen Nov 16 '22 at 21:33
  • The desired output is a String (in this case: "FD4936E20732A42E"). The child does not have a .value or .key in this context. The only thing I can access with "child." is .self. – Carlo Maryska Nov 18 '22 at 00:54
  • Did you see https://stackoverflow.com/a/56640036? – Frank van Puffelen Nov 18 '22 at 04:23
  • Yes but unfortunately that does not help because I cant use a [String:Any] dictionary here as the String is unknown during runtime. I only need to get the key "FD4936E20732A42E". – Carlo Maryska Nov 18 '22 at 14:18
  • `for case let child as FIRDataSnapshot in snapshot.children { guard let dict = child.value as? [String:Any] else {` – Frank van Puffelen Nov 18 '22 at 15:04
  • The else part gets triggered and I cannot use the dict :/ – Carlo Maryska Nov 18 '22 at 16:25
  • Ahh i have it. I needed to use .childAdded instead of .value in the header of the snapshot method :) – Carlo Maryska Nov 18 '22 at 16:32
  • Listening to `.childAdded` is not required. But if you listen to `.value`, you will need to loop over the children. – Frank van Puffelen Nov 18 '22 at 16:56

0 Answers0