0

I have this FirebaseDatabase structure:

enter image description here

I need to retrieve the data from Firebase. Usually I use a function like this:

funfunc getWodsFromDataBase () {
    print ("Get Wods From Data Base (AppDelegate)")
    let wodDb = Database.database().reference().child("wods")

    wodDb.observe(.childAdded) { (snapshot) in let snapValue = snapshot.value as! Dictionary<String, Any>



        let girl_hero = snapValue["girl_hero"]!
        let nombre = snapValue["nombre"]!
        let wod = Wod(
            girl_hero : girl_hero  as! String,
            nombre : nombre  as! String,
        )
        self.wodsArray.append(wod)
    }
}

Its easy to retrieve the first level values, but I don't know how to get every value of the second level ( exercices ) and how to update my wods struct that now are like this :

struct Wod {
    var girl_hero : String
    var nombre : String    
    init (){
        self.girl_hero = ""
        self.nombre = ""
    }
    init (
          girl_hero : String,
          nombre : String)
    {
        self.girl_hero = girl_hero
        self.nombre = nombre

    }
}

Can any one help me reWriting my Struct and getting the data from Firebase ?

Kargol
  • 113
  • 9

2 Answers2

1

Hope this may helpful for you. use the optional based on your requirements.

struct WordSelector:Codable {
    let word:[Exrcices]
}

struct Exrcices:Codable {
    let detail:[Deatil]
    let girl_hero: String
    let nombre: String
}

struct Deatil:Codable {
    let name: String
    let peso: String?
    let reps: String
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Rubiya Faniband
  • 310
  • 2
  • 10
  • But how can I put the data from Firebase ? can you show me the way ? – Kargol Oct 25 '19 at 10:49
  • In swift we can parse the data in one line if our struct confirms to Codable protocol, ex User user = dataSnapshot.getValue(WordSelector.class); same should be available in your language. Not sure whether it may help you – Rubiya Faniband Oct 25 '19 at 10:58
  • it helps me, but I don't know how to call the firebase getter to get this data in Swift – Kargol Oct 25 '19 at 11:18
  • https://firebase.google.com/docs/firestore/query-data/get-data chose swift option this may help you – Rubiya Faniband Oct 25 '19 at 11:30
1

It sounds like you want to loop over the children of the exercises node in your snapshot. You do that with something like this:

let wodDb = Database.database().reference().child("wods")

wodDb.observe(.childAdded) { (snapshot) in 
    let snapValue = snapshot.value as! Dictionary<String, Any>

    let girl_hero = snapValue["girl_hero"]!
    let nombre = snapValue["nombre"]!

    let exercises_snapshot = snapshot.childSnapshot(forPath: "exercises")

    for child in exercises_snapshot.children {
        let exercise_snapshot = child as! DataSnapshot
        let exercise_value = exercise_snapshot.value as! Dictionary<String, String>

        let exercise_name = exercise_value["name"]
        ...
    }
    ...
}

The new things in the above code:

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