olle
olle

Reputation: 144

Swift Fetching All Users To An Array

I am trying to fetch all users into an array and use callback when this is done. I need to know when the fetching is complete. I have understood that this might not work as i thought with childAdded so i am trying with observeSingleEvent but i am having problems to fetch into the array. Any ideas why?

This code with childAdded works with putting the data to the array but not the completion even if i put it inside.

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

                // Fetch users
                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)
             completion("FetchAllUsersKlar")

This code does not work at all:

self.ref?.child("Users").observeSingleEvent(of: .value, with: { (DataSnapshot) in

                // Fetch users
                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)

                }
                completion("FetchAllUsersKlar")
            })

Firebase looks like this:

enter image description here

Upvotes: 0

Views: 181

Answers (1)

Daniyal Raza
Daniyal Raza

Reputation: 372

This is the User struct:

struct User{
    var id:String
    var username:String?
    var occupation:String?
    var age:Int?
    var bio:String?
    var email:String?
}

Here's the firebase query, although I believe that decoding object through Codable is much better, but for the sake of sticking with your example I parsed the object with keys.

let query = self.ref.child("Users").queryOrdered(byChild: "email")
    query.observeSingleEvent(of: .value) {
        (snapshot) in
        for child in snapshot.children.allObjects as! [DataSnapshot] {
            let id = child.key
            let value = child.value as? NSDictionary
            let username = value?["Username"] as? String
            let occupation = value?["Occupation"] as? String
            let age = value?["Age"] as? Int
            let bio = value?["Bio"] as? String
            let email = value?["email"] as? String

            let user = User(id: id, username: username, occupation: occupation, age: age, bio: bio, email: email)

            self.usersArray.append(user)
        }
        completion("Users list fetched")
    }

Upvotes: 2

Related Questions