I have a switch statement that gets how many post a user has and I name the var numberOfPosts
to = posts.count
. But I would like to return the Int value from numberOfPosts
in the UICollectionView func numberOfItemsInSection
but it always returns zero. How do I go about fixing this?
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
userSubID = AWSMobileClient.default().userSub!
let post = uploads.keys
let predicate = post.userSub == userSubID
_ = Amplify.API.query(request: .list(uploads.self, where: predicate)) { event in
switch event {
case .success(let result):
switch result {
case .success(let posts):
self.numberOfPosts = posts.count
print(numberOfPosts) // It prints the num of posts
case .failure(let error):
print("Got failed result with \(error.errorDescription)")
}
case .failure(let error):
print("Got failed event with error \(error)")
}
}
print(numberOfPosts)
return numberOfPosts //The variable returns 0
}