GMGridView icon indicating copy to clipboard operation
GMGridView copied to clipboard

Image Async Download - Too many requests

Open anujgakhar opened this issue 12 years ago • 6 comments

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.

anujgakhar avatar Jul 09 '12 08:07 anujgakhar

anyone?

anujgakhar avatar Aug 07 '12 17:08 anujgakhar

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 avatar Aug 08 '12 07:08 banaslee

@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.

anujgakhar avatar Aug 08 '12 07:08 anujgakhar

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.

banaslee avatar Aug 08 '12 07:08 banaslee

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

exalted avatar Nov 12 '12 14:11 exalted

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.

Bejil avatar Jan 16 '14 11:01 Bejil