0

Is there a way to dynamically get a child database from the parent instance, without having to use childByAutoId () ?

{
  "artists" : {
    "artists_id_1" : {
      "name" : "Atif Aslam",
      "genre" : "Rock"
    },
    "artists_id_2" : {
      "name" : "Arijit Singh",
      "genre" : "Rock"
    }
  }
}

we usually refer to the path, and listener the items the DataBase...Example:

Database.database().reference().child("artists").child("artist_id_1").observeSingleEvent(of: .value, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
                print("\(String(describing: dictionary["name"] as? String))")
            }

        }, withCancel: nil)

it would only work for the first item. Because, it would not be dynamic. What is normally used is the following example below ...

{
  "artists" : {
    "-KeZUDrJv555kteAcssL-" : {
      "name" : "Atif Aslam",
      "genre" : "Rock"
    },
    "-KeZUVXFIQdO7JiyRYk-" : {
      "name" : "Arijit Singh",
      "genre" : "Rock"
    }
  }
}
 Database.database().reference().child("artists").childByAutoId().observeSingleEvent(of: .value, with: { (snapshot) in

            if let dictionary = snapshot.value as? [String: AnyObject] {
                print("\(String(describing: dictionary["name"] as? String))")
            }

        }, withCancel: nil)

Anyway, I do not know if it was clear, anything can comment before answering. Thank you

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Ewerson
  • 109
  • 1
  • 10
  • You've included pictures of JSON in your question. Please replace that with the actual JSON as text, which you can easily get by clicking the Export JSON link in [your Firebase Database console](https://console.firebase.google.com/project/_/database/data/). Having the JSON as text makes it searchable, allows us to easily use it to test with your actual data and use it in our answer and in general is just a Good Thing to do. – Frank van Puffelen Feb 08 '18 at 04:19
  • What are you trying to accomplish by calling `childByAutoId()` here? – Frank van Puffelen Feb 08 '18 at 04:20
  • Ok, @FrankvanPuffelen i'll edit the post, thanks. – Ewerson Feb 08 '18 at 11:52
  • @FrankvanPuffelen I'm not calling childByAutoId (), I just gave an example of how this call is normally done. In my case, instead of iDs Automaticos, I want to customize them. – Ewerson Feb 08 '18 at 12:02

2 Answers2

1

I dont know if i understand your question correctly or what you are trying to do but maybe you can try

Database.database().reference().child("artists").observe(.value) { (snap:DataSnapshot) in 

// This should print out your childIds
print(snap.key)

// This should print out the values after them
print(snap.value)



}
John Doe
  • 185
  • 2
  • 13
  • Hello John Doe, thanks for help. That's what I've tried to do. However, (snap.key) returns the main string "artists", I want it to return the item just below individually, instead of returning Optional ({artists = {.... – Ewerson Feb 08 '18 at 11:47
  • It may sound silly, but searching the internet, I did not find much about accessing the custom array string, because I usually use autoId or "uid" (unique user key). – Ewerson Feb 08 '18 at 11:49
  • In summary, I want to access the ID just below the reference "artists", that is, "artist_id_1". But I want this dynamically, I do not want to have to set this reference one by one, for example: ------------------------------------Database.database (). Reference (). Child ("artists"). Child ("artist_id_1")---------- – Ewerson Feb 08 '18 at 11:50
1

To display all artists in your database, load the artists node and loop over snapshot.children:

Database.database().reference().child("artists").observe(.value) { (snap:DataSnapshot) in 
  for child in snapshot.children {
    print(child.key)
  }
}

For this and more examples, I recommend reading the Firebase documentation, specifically the section on reading and writing lists.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807