下面的代码是我正在使用的代码,因此计数器值不会增加。当在闭包内部检索数据后立即打印出计数器时,它将打印我想返回的值。但是,在这一点上,我必须声明收益始终为0。如何解决该问题?
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
var counter: Int = 0
let userID = Auth.auth().currentUser?.uid
//Improve code block
Database.database().reference().child("subscriptions").child(userID!).observeSingleEvent(of: .value) { snapshot in
for child in snapshot.children.allObjects as! [DataSnapshot] {
let dict = child.value as! String
Database.database().reference().child("users").child(dict).child("hasStory").observeSingleEvent(of: .value) { snapshot in
if let item = snapshot.value as? Bool {
if (item == true) {
counter += 1
} else {
print("no story")
}
}
}
}
}
return counter
}