4

we are struggling to cancel request that already sent when we use collection view cell .. ( i am not talking about cases when we want to cancel request because we already sent the same request) ... i guess we need to use the methods: cancelRequestForRequestReceipt .. the problem is that it's not clear how to get this RequestReceipt.

Example:

We have a collection view with different images (each image have a different url) .. if i will scroll back and forward fast , the method af_cancelImageRequest will do the job and will not create 2 active request for the same url.. Super!, the problem is when we try to scroll only to 1 direction and we have 1000 images .. basically we want to be able to cancel the request that just sent, before the image will return, meaning we don't have the image and we don't AlamofireImage to continue with that request ... after the cell is disappear (cause by slow internet and fest scrolling ).. So, if i got it right , we can use cancelRequestForRequestReceipt ... the problem is that we can't find how to get this RequestReceipt ...

BTW: i saw the example code that AlamofireImage demo app,

override func prepareForReuse() {
    super.prepareForReuse()

    imageView.af_cancelImageRequest()
    imageView.layer.removeAllAnimations()
    imageView.image = nil
}

As i said, this code will cancel request for images ONLY if the request is already in the operation queue ..

Help :)

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
Sosily
  • 706
  • 1
  • 10
  • 26

1 Answers1

1

I think you're misunderstanding was AlamofireImage is doing in this example, but first let's understand what does the prepareForReuse method.

Every time the cell is going to be dequeued this method is called, this means that if for example as you said you scroll forward and backward fast this method will be called, if you scroll in one direction fast with n images this method will be called every time a cell disappears.

As you're calling the method over the UIImageView and exist a reference inside the UITableViewCell you don't need the RequestReceiptbecause the request is going to be canceled for you.

In the case when you make a fast forward and backward the correct way of handle it is not cancel it after a throttle preventing this kind of behaviour.

AlamofireImage it's a really great library but I think you can benefit much more using KingFisher, it handle for you the throtle in the case of fast forward and backward and of course the other case of the cell being dequeued and the request is no finished.

Nevertheless this libraries handle all the hard work for you and you don't need to reinvent the wheel my advice is you learn what's happens behind the scenes and how you can make it if these libraries doesn't exist yet.

I hope this help you.

Victor Sigler
  • 23,243
  • 14
  • 88
  • 105
  • Thanks @Victor Sigler ! i will check KingFisher :) – Sosily Sep 12 '16 at 06:23
  • @Victor Sigler, could you please point to the documentation (or better source file) which is responsible for "throttling" in KingFisher? – kean Sep 12 '16 at 07:40