Swift,如何让 Completion Handler 按我的意愿工作?

标签 swift firebase closures completionhandler

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

感谢您的帮助。

这是我的代码。我希望“fetchOneUser()”在“fetchAllUsers”完成时运行:

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

最佳答案

你需要使用 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")

            })
        }
    })
}

关于Swift,如何让 Completion Handler 按我的意愿工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55618730/

相关文章:

Javascript - 从外部访问函数闭包中的变量

ios - 覆盖 UIViewConrtoller 扩展中的 PreferredStatusBarStyle

ios - Swift 让 IBOutlet 变得强大

ios - 如何在同时处理捏合和平移手势识别器时更改触摸位置?

Firebase 托管上的 Firebase 功能 : 404

android - Android Firebase实现不正确

reactjs - 当 firebase 配置发生变化时更新 firebase 消息服务 worker

php - 作为参数传递时如何将 PHPDoc 匿名函数?

javascript - 在 python 中关闭。我可以关闭函数的局部上下文吗?

ios - 在 Sprite Kit 中使用 3D touch 测量压力