Swift,如何完成Handler工作,我希望?

问题描述 投票:0回答:2

我从Firebase获取用户信息,我的目标是获取一批用户,然后在卡片堆栈中一次一个地显示在屏幕上。为此,我使用完成处理程序。但是,完成处理程序中的代码在完成所有用户的提取之前运行。

感谢您的任何帮助。

这是我的代码。我想在“fetchAll Users”完成后运行“fetchOne User()”:

fetchAllUsers(completion: { message in
   print(message)
   print("FetchOneUser")
   self.fetchOneUser()
})

这是fetchAllUser函数:

func fetchAllUsers(completion: @escaping (_ message: String) -> Void){
        //User or advertiser?
        Database.database().reference(withPath: "Advertiser").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in

            if snapshot.exists(){
                myAdvertiserVar.advertiser = true
                self.currentUserKind = "Advertiser"
                self.otherUserKind = "Users"
            }
            else{
                self.currentUserKind = "Users"
                self.otherUserKind = "Advertiser"
            }

            // Fetch
            let query = self.ref?.child(self.otherUserKind).queryOrdered(byChild: "email")
            query?.observeSingleEvent(of: .value) {
                (snapshot) in
                for child in snapshot.children.allObjects as! [DataSnapshot] {
                    let id = child.key
                    //If Already Accepted, don't fetch
                    Database.database().reference(withPath: self.currentUserKind).child(self.uid).child("Accepted").child(id).observeSingleEvent(of: .value, with: {(accepted) in
                        if accepted.exists(){
                            print("\(id) är redan Accepted")
                        }
                        else{
                            if myAdvertiserVar.advertiser == true{
                                let value = child.value as? NSDictionary
                                let username = value?["Username"] as? String
                                let occupation = value?["Occupation"] as? String
                                let age = value?["Age"] as? String
                                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)
                            }
                            else{
                                let value = child.value as? NSDictionary
                                let username = value?["Owner"] as? String
                                let occupation = value?["Location"] as? String
                                let age = value?["Rent"] as? String
                                let bio = value?["About"] 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)
                            }
                        }
                    })
                }
                print(self.usersArray.count)
                completion("Users list fetched")
            }
        })
    }
swift firebase closures completionhandler
2个回答
2
投票

你需要使用DispatchGroup,因为内部调用是异步的

func fetchAllUsers(completion: @escaping (_ message: String) -> Void){
    //User or advertiser?
    Database.database().reference(withPath: "Advertiser").child(uid).observeSingleEvent(of: .value, with: { (snapshot) in

        if snapshot.exists(){
            myAdvertiserVar.advertiser = true
            self.currentUserKind = "Advertiser"
            self.otherUserKind = "Users"
        }
        else{
            self.currentUserKind = "Users"
            self.otherUserKind = "Advertiser"
        }

        // Fetch
        let query = self.ref?.child(self.otherUserKind).queryOrdered(byChild: "email")
        query?.observeSingleEvent(of: .value) {
            (snapshot) in

            let g = DispatchGroup()

            for child in snapshot.children.allObjects as! [DataSnapshot] {
                let id = child.key
                //If Already Accepted, don't fetch

                g.enter()

                Database.database().reference(withPath: self.currentUserKind).child(self.uid).child("Accepted").child(id).observeSingleEvent(of: .value, with: {(accepted) in
                    if accepted.exists(){
                        print("\(id) är redan Accepted")
                    }
                    else{
                        if myAdvertiserVar.advertiser == true{
                            let value = child.value as? NSDictionary
                            let username = value?["Username"] as? String
                            let occupation = value?["Occupation"] as? String
                            let age = value?["Age"] as? String
                            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)
                        }
                        else{
                            let value = child.value as? NSDictionary
                            let username = value?["Owner"] as? String
                            let occupation = value?["Location"] as? String
                            let age = value?["Rent"] as? String
                            let bio = value?["About"] 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)
                        }
                    }

                    g.leave()
                })
            }


            g.notify(queue: .main, execute: {

                print(self.usersArray.count)
                completion("Users list fetched")

            })
        }
    })
}

1
投票

基于Firebase documentation:

Firebase使用qazxsw poi方法获取实时数据库异步根的数据库引用。

这意味着结果需要更多的时间来获取而不是for循环,在这种情况下你的for循环完成和完成块调用并将你带出方法,然后你的请求的结果将返回。

你的代码应该是这样的

refrence()
© www.soinside.com 2019 - 2024. All rights reserved.