Firebase data into an array of dictionaries. iOS, Swift

I am trying to extract data from a firebase database and store the result in an array of dictionaries. I want to save data this way ....

[[zip code: zip code, latitude: 87.28764862, longitude: -23.94679475694, registration: registration], [zip code: zip code, latitude: 87.28764862, longitude: -23.94679475694, registration: registration], [zip code: zip code, latitude: 87.28764862, longitude: -23.94679475694, registration: registration]]

As a result, I get ...

Tony: before adding the message [] 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

data of my firebase ... Firebase Data

If I add one more to my firebase, I get this result ...

+4
1

, .

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")
                        }
                    })
                }
            })
+2

Source: https://habr.com/ru/post/1683621/


All Articles