0

When loading my UICollectionView cells I call a method which downloads an image async.

During the download however, my collection view is reloaded and so when the async image is downloaded it is set in two different cells.

I have also tried using an NSOperationQueue where in dealloc I call cancelAllOperations:, but this didn't work.

What is the best way to cancel this download and can someone provide some sample code?

Thanks.

Irfan
  • 4,301
  • 6
  • 29
  • 46
Adam Carter
  • 4,741
  • 5
  • 42
  • 103

1 Answers1

0

I think the best practice is to launch requests lazily as you need the images, cache the results, and have no expectations about the state of the collection when the request completes.

Reloading the collection is not an invalidating event for a request for an image in a certain cell. Scrolling away is, but the user might scroll back. So make the request, and in the completion block for the request cache the result and reloadItemsAtIndexPaths: on the index path associated with the request.

My answer here, provides working code.

Community
  • 1
  • 1
danh
  • 62,181
  • 10
  • 95
  • 136
  • The problem seems like it should be a lot easier than that to me? I can't understand why there's still a pointer to the `UIImageView` at the point the image has downloaded. – Adam Carter Apr 07 '14 at 00:58
  • You might need to post the code. Also, are you saying that you do one assignment but see the image views in change in two cells? (That sounds super unlikely. It would be much more likely that two requests that seem to you to be distinct are resulting in the same image). – danh Apr 07 '14 at 01:37
  • There's a sequence of events. Get cell -> Async load image... Cells reload and some are added -> Get cell -> sync load image -> load image one in cell of same index path as first load -> load second image in cell of same index path as second load. – Adam Carter Apr 07 '14 at 01:57