0

I am trying to retrieve a dictionary from Firebase and extract each value from the dictionary and append it to an empty array, but my code doesn't work. I haven't even added the code for appending it to an array and when I run it, "error" is printed in the console.

This is what it looks like inside Firebase

And this is what my code looks like:

    func convertAllMosaicsToArray() {
    // retrieve and convert here

    Database.database().reference().child("mosiacTitles").observe(.value, with: { (snapshot) in

        if let dictionary = snapshot.value as? [Int : AnyObject] {
            print(dictionary)
        } else {
            print("error")
        }
    })

}
Ben Nalle
  • 537
  • 2
  • 8
  • 21
  • firebase uses json, it supports arrays – rmickeyd Oct 11 '17 at 19:00
  • @rMickeyD Wrong: "The Firebase Database doesn't store arrays. It stores dictionaries/associate arrays." https://stackoverflow.com/a/40055996/7715250. If you want to use Firebase with arrays, you need to use FireSTORE – J. Doe Oct 11 '17 at 19:03
  • https://firebase.google.com/docs/database/ios/read-and-write @J.Doe It may not be an array when stored in firebase but on client side in swift you can set an array – rmickeyd Oct 11 '17 at 19:08
  • @rMickeyD I don't need to write the data via the client side, I just need to be able to read the data from the client side. There is a way to write data to the Firebase database from within the Firebase console, but I don't know how to read that data and use it like it's an array on the client side – Ben Nalle Oct 11 '17 at 19:17
  • @J.Doe While I'm all for pointing developers for Firestore, this is not necessarily a good case. As rMickeyD says, the Firebase client shows the data as arrays in many cases. – Frank van Puffelen Oct 12 '17 at 03:46
  • @BenNalle [This blog post](https://firebase.googleblog.com/2014/04/best-practices-arrays-in-firebase.html) explains in detail how the Firebase Realtime Database deals with arrays, and why you should consider carefully if they are the right data structure for your needs. The majority of the usage of arrays I see, should actually be a set. For more on that, see my answer here: http://stackoverflow.com/questions/40656589/firebase-query-if-child-of-child-contains-a-value – Frank van Puffelen Oct 12 '17 at 03:48

2 Answers2

0
    static func load(_ completion: @escaping () -> ()) {
    if let user = Auth.auth().currentUser {
        let ref = Database.database().reference().child("users").child(user.uid).child("something")
        ref.observe(.value, with: { (snapshot) in

            if let data = snapshot.value as? [String : AnyObject] {

                var arrayNeeded: [Int] = []

                if let array = data["key"] as? [Int] {
                        arrayNeeded = array
                }

            }
            completion()

        })
    }
}
rmickeyd
  • 1,551
  • 11
  • 13
  • I tried your answer but I am unable to access the data from the snapshot. I also updated my question with a picture of the database in Firebase. – Ben Nalle Oct 12 '17 at 17:29
  • To be clear, my answer was correct. The data you needed is just one level higher than the example I provided. – rmickeyd Oct 12 '17 at 21:30
0

The problem was using the if let statement to cast it as a [Int : AnyObject], it just needed to be changed to an [String]

Like this:

    func retrieveMosaicTitles() {
    // retrieve and convert here

    Database.database().reference().child("mosaicTitles").observe(.value, with: { (snapshot) in

        if let allMosaicTitles = snapshot.value as? [String] {
            self.searchResultsVC.listOfMosaics = allMosaicTitles
        } else {
            print("error")
        }
    })
}
Ben Nalle
  • 537
  • 2
  • 8
  • 21