This is awesome!! Thank you so much, I needed to fix my infinite scrolling collection view so you came in just in time .
I have a question though, why instruct the tableView to render ALL of the contents? Would this still work if we updated the size of the datastore as the new items come in from the request? In my case showing empty cells is not optimal nor desired.
The point of rendering all of the contents is to avoid paging. You want to know ahead of time how much data is being received, so you’re able to prepare the UITableView how many cells it needs to display, before they appear to the user.
Your case of, “showing empty cells is not optimal or desired” is the situation for everyone No one wants to wait, nor see an empty UITableViewCell when they are scrolling. Because we’re using UITableView’s prefetch methods, these cells are prepared ahead of time, and the user should see a smooth scrolling experience. The empty cells with a spinner appear depend on a variety of factors:
How much data is being downloaded from the server
How fast is the user scrolling
How fast the network connection is
In the case of dealing with images from the server, it would make sense for you to say, cache the images locally so that unnecessary network calls are not made once you’ve downloaded them once already. The key to remember here is that you’re trying do the prefetch the data in the background while rendering the UI to the user on the main thread, thus trying to achieve a seamless experience for the user. In your solution, what you’re suggesting should be possible, however, I think you’re creating more work for yourself than necessary since you’ll always be updating the internal datastore every time the user scrolls.
From the users perspective, continuous scrolling is all about a smooth experience on the device
From the developer’s perspective, it is about providing both the smooth experience on the front end and also ensuring that the backend supports the data retrieval for the same.
So, as a developer you need to pull all the good practices to ensure you use things like caching, queues, background threads, pre-fetching, etc
If you have more data, you can pre-fetch or prepare for more related data accordingly.
The materials to download with this example do not seem to have the same code. For example searching the downloaded materials for: “DispatchQueue.main.” yields no results.
Oh no. How come? Every field, every technology, every area of study has roadblocks on your path to learning. If you’re not stuck, you’re not challenging yourself. Please do not give up, but rather, be persistent, ask others around you, post your questions online, and eventually, you will get the help you need