Arrays 快速将所有用户提取到一个数组

Arrays 快速将所有用户提取到一个数组,arrays,swift,firebase-realtime-database,fetch,Arrays,Swift,Firebase Realtime Database,Fetch,我正在尝试将所有用户提取到一个数组中,并在完成此操作时使用回调。我需要知道提取什么时候完成。我知道这可能不会像我对childAdded所想的那样起作用,所以我尝试使用observeSingleEvent,但我在获取数组时遇到问题。你知道为什么吗 这段带有childAdded的代码用于将数据放入数组,但即使我将其放入数组中也无法完成 self.ref?.child("Users").observe(DataEventType.childAdded, with: { (snapshot) in

我正在尝试将所有用户提取到一个数组中,并在完成此操作时使用回调。我需要知道提取什么时候完成。我知道这可能不会像我对childAdded所想的那样起作用,所以我尝试使用observeSingleEvent,但我在获取数组时遇到问题。你知道为什么吗

这段带有childAdded的代码用于将数据放入数组,但即使我将其放入数组中也无法完成

  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")
此代码根本不起作用:

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看起来像这样:


这是用户结构:

struct User{
    var id:String
    var username:String?
    var occupation:String?
    var age:Int?
    var bio:String?
    var email:String?
}
这是firebase查询,虽然我相信通过Codable解码对象要好得多,但为了坚持使用您的示例,我用键解析了对象

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

这是用户结构:

struct User{
    var id:String
    var username:String?
    var occupation:String?
    var age:Int?
    var bio:String?
    var email:String?
}
这是firebase查询,虽然我相信通过Codable解码对象要好得多,但为了坚持使用您的示例,我用键解析了对象

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

您应该在完成时使用对象在完成时返回值。一些有用的文章,我相信在这种情况下,在对象用户上使用protocolcodable是非常受欢迎的。您应该在完成时使用对象,在完成时返回值。一些有用的文章,我相信在这种情况下,在对象用户上使用可编码的协议是非常受欢迎的。