Ler Ws Ler Ws - 2 months ago 29
Swift Question

Storing data from firebase child key value pairs as an array

How do I store key value pairs from firebase into an array? I've tried writing a code to send some data to firebase from a local array and noticed they are stored in such form:

enter image description here

notice how the 'medals' child are stored.

I'm trying to take a snapshot of the array from the 'medals' object and return it as an array in swift.

let databaseRef = FIRDatabase.database().reference()
databaseRef.child("users").child(userID!).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
// Get user medals
self.identities3 = snapshot.value!["medals"] as! [String]

})


I know this is very crude, but would it work?

Thanks! Still pretty new to firebase and learning :)

Answer

Try this:

let databaseRef = FIRDatabase.database().reference()
    databaseRef.child("users").child(userID!).child("medals").observeSingleEventOfType(.Value, withBlock: { (snapshot) in
        // Get user medals
        self.identities3 = snapshot.value as! [String]

    })