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

Swift, How to Get Completion Handler working as I Wish?

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

感谢您的帮助。

这是我的代码。当 "fetchAllUsers" 完成时,我希望 "fetchOneUser()" 到 运行:

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

            })
        }
    })
}

基于Firebase documentation:

Firebase 使用 refrence() 方法为您的实时数据库异步获取根目录的数据库引用。

这意味着获取结果比 for 循环花费更多的时间,在这种情况下,您的 for 循环结束并且完成块调用并使您退出方法,然后您的请求结果将 return.

您的代码应如下所示

var firebaseDatabaseRefrence: DatabaseReference!

override func viewDidLoad() {
   super.viewDidLoad()
   Database.database().reference(withPath: self.currentUserKind)
}

func someMethod() {

  self.firebaseDatabaseRefrence
  .child(self.uid)
  .child("Accepted")
  .child(id).observeSingleEvent(of: .value, with: {(accepted) in 

}