我尝试使用某个字典的numberOfItemsInSection
设置我的集合视图的count
方法。字典是通过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)
})
}
答案 0 :(得分:1)
设置数组
后使用此功能collectionView.reloadData()
(在for循环之后)