GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
Already on GitHub? Sign in to your account
I have a Grid View with possibly upto 1000 Cells. Each Cell displays an Image that gets downloaded asynchronously if it has not already been downloaded from the server. Now, this works if the images are a few, but if there are e.g. 1000 cells and I scroll very quickly from top to bottom, this triggers off 1000 Network requests to download all the images. And obviously, that can cause the app to crash.
What is the best solution to avoid this scenario ? Is it OK to implement UIScrollViewDelegate and only download the images if the scrolling has stopped i.e. do not download if the user is still scrolling ?
Or some other solution would be advisable?
Thanks in advance.
If you implement the UIScrollViewDelegate and start downloading only after the scrolling has stopped how do you know which are the visible cells so that you launch only the right requests?
You can create a small class for the contents of the cell that only launch the request some milliseconds after the view appears. If the cell disappears and consequently its content view is released from memory then the request will never be launched.
This way you don't need to know which cells are visible after the scroll stopped.
@banaslee Not sure that would work efficiently as the user could be scrolling up and down fast and the network requests would still go out in this case. What would be ideal is something like indexPathsForVisibleRows in UITableView which we can use after the scrolling stops.
I agree that a method for getting the visible rows is also a very good addition (I also need something like that) and of course it can help you achieve what you want.
Implementing a method like that it's not that difficult if you want to do it quickly for yourself. Just look at GMGridView loadRequiredItems private method.
There are literally tons of implementations for this, please pick one, either one, just don't re–invent the wheel:
I reached to get visible cells : #43
So now, thanks to Apple LazyTableImages technique (https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html) you can download asynchronously your images.