0
我一直在搞調度組,並想知道調度組的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()
})
}
})
}
當通知位於原始數據庫讀取塊內時,此代碼行爲正常。但是,如果我在ref.observeSingleEvent
塊之後放置該通知,則立即調用通知。
有人可以向我解釋這個嗎?
如果您有它的塊則通知外被之前達成的任何你的電話到'self.dispatchGroup.enter()'到達。 – dan