GMGridView
GMGridView copied to clipboard
Image Async Download - Too many requests
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.
anyone?
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:
- http://latest.docs.nimbuskit.info/NINetworkImageView.html
- https://github.com/rs/SDWebImage
- http://afnetworking.github.com/AFNetworking/Categories/UIImageView+AFNetworking.html
I reached to get visible cells : https://github.com/gmoledina/GMGridView/issues/43?source=cc So now, thanks to Apple LazyTableImages technique (https://developer.apple.com/library/ios/samplecode/LazyTableImages/Introduction/Intro.html) you can download asynchronously your images.