Tony Merritt
Tony Merritt

Reputation: 1197

Firebase data into an Array of dictionaries. iOS, Swift

I'm trying to retrieve data from a firebase database and store the result in an array of dictionaries. I want to store the data in this way....

[[postcode: postcode, latitude: 87.28764862, longitude: -23.94679475694, registration: registration], [postcode: postcode, latitude: 87.28764862, longitude: -23.94679475694, registration: registration], [postcode: postcode, latitude: 87.28764862, longitude: -23.94679475694, registration: registration]]

The result I'm getting is...

Tony: before append post [] Tony: post [appname.Post]

Where am I going wrong?

var places = [String]()
var postsNew = [Post]()
var posts = [String: Any]()

DataService.ds.REF_POSTS.observe(.value, with: { (snapshot) in
        if snapshot.hasChild("postCodes") {
            let refToUser = DataService.ds.REF_POSTS
            refToUser.child("postCodes").observe(.value, with: { snapshot in

                let value = snapshot.value as? NSDictionary
                let postsIds = value?.allKeys as! [String]
                for postId in postsIds {
                    let refToPost = Database.database().reference(withPath: "posts/" + "postCodes/" + postId)
                    refToPost.observe(.value, with: { snapshot in
                        if snapshot.exists() {

                            let postDict = snapshot.value as? Dictionary<String, AnyObject>
                            let pCodeData = snapshot.key
                            let post = Post(postKey: pCodeData, postData: postDict!)


                            self.configureCell(post: post)
                            print("Tony: before append post \(self.postsNew)")
                            self.postsNew.append(post)
                            print("Tony: post \(self.postsNew)")


                }else {
                            print("Tony: Couldn't get the data")
                        }
                    })
                }
            })
        }else {
            print("Tony: No Favs added, couldn't get the data")
        }

    })


func configureCell(post: Post) {
    self.postData = post

    self.latitude = post.latitude
    self.longitude = post.longitude
    self.postCodeDB = post.postCode
    self.registration = post.other2

}

and in my Post class..

    init(postKey: String, postData: Dictionary<String, AnyObject>) {
    self._postKey = postKey

my firebase data...Firebase data

If I add another one to my firebase I get this result...

Upvotes: 4

Views: 1990

Answers (1)

Tony Merritt
Tony Merritt

Reputation: 1197

I found the answer by reviewing my code.

DataService.ds.REF_POSTS.child("postCodes").observe(.value, with: { (snapshot) in

                let value = snapshot.value as? NSDictionary
                let postsIds = value?.allKeys as! [String]
                for postId in postsIds {
                    let refToPost = Database.database().reference(withPath: "posts/" + "postCodes/" + postId)
                    refToPost.observe(.value, with: { snapshot in
                        if snapshot.exists() {

                            let postDict = snapshot.value as? [String: AnyObject]

                            print("Tony: before append post \(self.posts)")
                            self.posts.append(postDict!)
                            print("Tony: post \(self.posts)")
                            self.showSightingsOnMap()

                }else {
                            print("Tony: Couldn't get the data")
                        }
                    })
                }
            })

Upvotes: 2

Related Questions