0
我正在嘗試使用某個字典的count
來設置我的收集視圖的numberOfItemsInSection
方法。字典是從Firebase調用中設置的(如果該部分很重要,請在下面的代碼中進行設置)。我感覺Firebase調用是異步的,並且不需要與調度隊列,閉包或單獨的完成處理程序結合使用。只有在Firebase調用完成後才設置收集視圖方法
但是,當我嘗試將numberOfItemsInSection
設置爲return avatarDictionary.count
時,它是空的,並且確實打印該計數顯示爲0.所討論的字典確實使用其值設置(打印確認),但它需要遍歷所有用戶在獲取所有數據之前先獲取數據。我想當numberOfItemsInSection
檢查其return
時,字典仍然在0.
這是怎麼回事?如果是這樣,在設置集合視圖之前,確保字典完全以其所有值設置的最佳方法是什麼?
代碼:
func getParticipantInfo() {
let databaseRef = FIRDatabase.database().reference()
// Query Firebase users with those UIDs & grab their gender, profilePicture, and name
databaseRef.child("groups").child(currentRoomID).child("participants").observeSingleEvent(of: .value, with: { (snapshot) in
if let snapDict = snapshot.value as? [String:AnyObject] {
for each in snapDict {
let uid = each.key
let avatar = each.value["profilePicture"] as! String
let gender = each.value["gender"] as! String
let handle = each.value["handle"] as! String
let name = each.value["name"] as! String
// Set those to the dictionaries [UID : value]
self.avatarDictionary.setValue(avatar, forKey: uid)
self.nameDictionary.setValue(name, forKey: uid)
self.genderDictionary.setValue(gender, forKey: uid)
self.handleDictionary.setValue(handle, forKey: uid)
print("\n\navatarDictionary:\n \(self.avatarDictionary)")
print("\nhandleDictionary:\n \(self.handleDictionary)")
print("\ngenderDictionary:\n \(self.genderDictionary)")
print("\nnameDictionary:\n \(self.nameDictionary)")
}
}
}, withCancel: {(Err) in
print(Err.localizedDescription)
})
}
不知怎的,我總是忘了重裝後。謝謝! – KingTim