UIImageView Caching Policy - AFNewtorking

I have several UIImageView in my application that I use AFNetworking setImageWithURL to populate the image from my server. The documentation says that this method uses the standard UIImageView cache policy. My problem is that if I update the image on my server, the image will not be updated in the application soon enough. Even if I close the application and reboot. If I want the images to be updated, I have to reinstall the application, which clears the cache and introduces new images. Obviously, this is not the desired effect for the user.

The AFNetworking cache is closed, so I do not have direct access to it in the UIImageView + AFNetworking category. Any thoughts on clearing the entire application cache? How can i achieve this?

+2
source share
1 answer

You can access the + (AFImageCache *)af_sharedImageCache by simply copying it to the UIImageView+AFNetworking.h interface file. This will open a subclass of single-user NSCache. You can then call – removeObjectForKey: or – removeAllObjects to remove the image or all images, respectively.

If you do not want to do this, consider that the image is cached using the NSHTTPURLResponse object as the key. You can create another key by adding some random value to the URL (for example, you can add ?garbage=4H5G789H35G89H ). However, this will mostly lose memory, as you will cache images that will never be recovered.

Finally, you may want to send an improvement request (or the code itself is one) to open the cache or make caching optional.

+2
source

Source: https://habr.com/ru/post/1500165/


All Articles