I have a tableView. Whenever it displays its last cell it loads more content into it (older posts).
If there's no more posts, then the app sets a bool which prevents more load requests.
However, if the user scrolls up-down fast a few times, the app gets a lot of load requests an crashes.
I would like to prevent that, by using a bool:
So if the app gets a new load-request while the first load-request is still on-going, then I'd like to cancel it.
Bools are working fine, but I heard that semaphores are safer / better.
Should I use semaphores? If so, is there a way to not to put a semaphore into waiting, instead if there's an already ongoing process, cancel?
func loadPosts(){
if (isLoadingMoreScrollContent){
return
}
self.isLoadingMoreScrollContent = true // THE BOOL
self.activityIndicator.startAnimating()
self.profileFunctions.loadMorePosts(lastLoadedPostId: self.lastLoadedPostId) { response, posts in
DispatchQueue.main.async {
if let response = response {
bla bla... adding the posts
}
self.activityIndicator.stopAnimating()
self.isLoadingMoreScrollContent = false // THE BOOL
}
}
}