1

Hey guys I have this setup of an array of custom objects :

 let posts : [Post] = {
    // fill posts array with posts from all buddys "privataPosts only"
    var ret = [Post]()
    staticValuesForData.instance.dataBaseUserref.child((Auth.auth().currentUser?.uid)!).child("contacts").observe( .value , with: { (snapshot) in

        let dict = snapshot.children.allObjects as! [DataSnapshot]
        for d in dict{
            if let contactUid = d.childSnapshot(forPath: "uid").value as? String{


            staticValuesForData.instance.dataBaseUserref.child(contactUid).child("privatePosts").observe( .value , with: { (snapshot) in

                let posts = snapshot.children.allObjects as! [DataSnapshot]
                print("postval" , posts)

                    for post in posts{

                        if let dict = post.value as? [String : AnyObject]{
                            let fullname = dict["fullname"] as! String
                            let picUrl = dict["picUrl"] as! String
                            let postContent = dict["postContent"] as! String
                            let time = dict["time"] as! Int
                            let uid = dict["uid"] as! String
                            let username = dict["username"] as! String

                            print("first name of person who did the post" , fullname)

                            let reposts = dict["reposts"] as! [String]

                            let downs = dict["downs"] as! [String]
                            // possible issue
                            var comments = [Comment]()

                            let commentArr = snapshot.childSnapshot(forPath: "comments").children.allObjects as! [DataSnapshot]

                            for c in commentArr{
                                if let dict = c.value as? [String : AnyObject]{

                                    let cuid = dict["uid"] as! String
                                    let ccommentText = dict["commentText"] as! String
                                    let cpicUrl = dict["picUrl"] as! String
                                    let cusername = dict["username"] as! String
                                    let ctime = dict["time"] as! Int

                                    let com = Comment(uid: cuid, commentText: ccommentText, time: ctime, picUrl: cpicUrl, username: cusername)

                                    comments.append(com)

                                }

                            }

                            print("HERE : post content\(postContent) username : \(username) commentArr \(comments)")

                            let postToAdd = Post(postContent: postContent, picUrl: picUrl, userName: username, fullName: fullname, postID: uid, postTime: time, downs: downs, reposts: reposts, comments: comments)

                           print("LOOK AT MEE   \(postToAdd.userName) is the username of the post object \(postToAdd.postContent) is the contetn")

                            ret.append(postToAdd)
                            print("RET" , ret)
                        }
                    }
                })

            }
        }

    })

    return ret
}()

and at the print statement "RET" , ret I see this

RET [Free.Post]

RET [Free.Post, Free.Post]

RET [Free.Post, Free.Post, Free.Post]

In the console so ik the "ret" array is populated with the proper objects , yet when I return and do something like

  override func numberOfItems(_ section: Int) -> Int {
    print("COUNT " , posts.count)
    return posts.count

}

"COUNT" , posts.count gives me 0 ... what is going on? why is my posts array not being populated??

rmaddy
  • 314,917
  • 42
  • 532
  • 579
zackkkkkkk
  • 25
  • 5
  • A quick glance makes me suspect you are falling into the "async closure" trap. Yes the closure populates it correctly, but it's async, so when you try to print it in your method, it isn't done yet. – ghostatron Jan 12 '18 at 17:10
  • ugh I had a hunch @ghostatron any idea on how to get around this? – zackkkkkkk Jan 12 '18 at 17:17
  • Do you get RET [Free.Post] in the log printed after COUNT 0 ? – claude31 Jan 12 '18 at 17:36

0 Answers0