Ios 调度组通知安置?

Ios 调度组通知安置?,ios,swift,firebase,swift3,grand-central-dispatch,Ios,Swift,Firebase,Swift3,Grand Central Dispatch,我一直在搞调度组,我想知道调度组的notify回调的位置如何影响调用回调的时间。我正在从数据库中读取数据,然后为读取的数据中的每个项目提取一张图片。当notify位于初始数据库读取块之外时,我注意到它会立即被调用,但是当notify位于块内部时,它的行为是正确的。这是我的密码: override func viewDidAppear(_ animated: Bool) { let ref = FIRDatabase.database().reference().child("us

我一直在搞调度组,我想知道调度组的notify回调的位置如何影响调用回调的时间。我正在从数据库中读取数据,然后为读取的数据中的每个项目提取一张图片。当notify位于初始数据库读取块之外时,我注意到它会立即被调用,但是当notify位于块内部时,它的行为是正确的。这是我的密码:

override func viewDidAppear(_ animated: Bool) {
        let ref = FIRDatabase.database().reference().child("users").child((FIRAuth.auth()?.currentUser?.uid)!).child("invites")
        ref.observeSingleEvent(of: .value, with: { snapshot in
            if let snapshots = snapshot.children.allObjects as? [FIRDataSnapshot] {
                for child in snapshots {
                    self.dispatchGroup.enter()
                    let info = petInfo(petName: child.value! as! String, dbName: child.key)
                    print(info)
                    self.invitesData[info] = UIImage()
                    let picRef = FIRStorage.storage().reference().child("profile_images").child(info.dbName+".png")
                    picRef.data(withMaxSize: 1024 * 1024) { (data, error) -> Void in
                        if error != nil {
                            print(error?.localizedDescription ?? "Error getting picture")
                        }
                        // Create a UIImage, add it to the array
                        self.invitesData[info] = UIImage(data: data!)!
                        self.dispatchGroup.leave()
                    }
                }
                self.dispatchGroup.notify(queue: DispatchQueue.main, execute: {
                    print("YOOO")
                    self.invitesArray = Array(self.invitesData.keys)
                    print(self.invitesArray)
                    self.inviteTable.reloadData()
                })
            }
        })
    }
当notify在原始数据库读取块内时,此代码正常运行。但是,如果我将其放在
ref.observeSingleEvent
块之后,就会立即调用notify

谁能给我解释一下吗?

是的。异步代码:-)


代码执行一直运行到函数结束,然后将调用完成处理程序

如果在块外,则在到达对self.dispatchGroup.enter()的任何调用之前到达notify。