0

I am appending user info from Firebase into an array but it seems like the array(usersArray) is empty in all prints after the loop. The array is declared outside the function. Any ideas why? Thank you.

    func fetchAllUsers(completion: @escaping (_ message: String) -> Void){

    //User or advertiser?
    let uid = Auth.auth().currentUser?.uid

    Database.database().reference(withPath: "Advertiser").child(uid!).observeSingleEvent(of: .value, with: { (snapshot) in

        if snapshot.exists(){

            myAdvertiserVar.advertiser = true

            // Fetch all users
            self.ref?.child("Users").observe(DataEventType.childAdded, with: { (snapshot) in


                if let dictionary = snapshot.value as? [String: AnyObject] {
                    let user = User()
                    user.nameLabel = dictionary["Username"] as? String
                    user.occupationLocation = dictionary["Occupation"] as? String
                    user.ageLabel = dictionary["Age"] as? String
                    user.infoText = dictionary["Bio"] as? String
                    user.emailText = dictionary["email"] as? String

                    let email = user.emailText
                    let ref = Database.database().reference().child("Users")
                    ref.queryOrdered(byChild: "email").queryEqual(toValue: email).observeSingleEvent(of: .childAdded, with: { (snapshot) in
                        user.toId = snapshot.key

                    })

                    self.usersArray.append(user)

                }
            })
            dump(self.usersArray)   //This print is empty
            completion("FetchAllUsers")

        }
}
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20
olle
  • 134
  • 3
  • 15
  • https://stackoverflow.com/a/44490781/5009432. Reference this -- your difference and this is that you need to add the completion INSIDE your observe function, whereas it will know when it is done. Yours does not. – impression7vx Nov 15 '18 at 19:00
  • This did not solve it for me, It only produces prints until the looping has ended. Not only one single print and one completion message as i wish for. Do you know why? – olle Nov 15 '18 at 19:28
  • Yea, one sec. Gonna add answer. – impression7vx Nov 15 '18 at 19:31
  • https://stackoverflow.com/a/42615219/5009432. Here, this shows you how to determine when Firebase is done calling. You can't do it your way as you are . observing through each child, and you never know which 1 is the last. – impression7vx Nov 15 '18 at 19:41
  • 1
    You are missing the concept of "asynchronism". If you add `print("will append")` just before `self.usersArray.append(user)`, and `print("will call completion")` just before `completion("FetchAllUsers")` you'll see that the order of the output is not the one you think of... – Larme Nov 15 '18 at 19:48
  • @impression7vx tried this but couldn't figure out how to get it to work with my code, it is a bit different. – olle Nov 15 '18 at 20:07
  • The way you are doing it does this -> `When child is added -> Do something`. There is not a way to determine when ALL children finish (at least not that I can think of). This would be good if you wanted to do something PER CHILD. – impression7vx Nov 15 '18 at 20:12
  • So do you mean i can use DispatchQueue for this or not? – olle Nov 16 '18 at 11:25

0 Answers0