0

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                
        }
    }
}
Kárpáti András
  • 1,221
  • 1
  • 16
  • 35
  • I think in your case bool is perfect. – aBilal17 Jul 24 '18 at 11:02
  • 1
    Compare https://stackoverflow.com/questions/49632941/what-is-the-purpose-of-semaphore-waittimeout-now – Martin R Jul 24 '18 at 11:16
  • To my understanding, you are going to introduce unnecessary complexity for a simple use-case. Take a look at [this answer](https://stackoverflow.com/a/37760230/3687801) which is almost same as your requirement. – nayem Jul 25 '18 at 02:20

0 Answers0