Ler Ws Ler Ws - 1 year ago 161
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 Source

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]

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download