Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching in AFNetworking+UIImageView doesn't respect any http cache policy? (hopefully I'm wrong)

It seems if I call:

setImageWithURLRequest:(NSURLRequest *)urlRequest
              placeholderImage:(UIImage *)placeholderImage
                       success:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image))success
                       failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error))failure

That the resulting image will be put into AFNetworking's AFImageCache regardless of any cache policy set in HTTP response headers and will remain there and be fetched from there regardless of age, on subsequent requests to the same URL.

Is that accurate?

The only thing that forces it to ignore the cache, is to set NSURLRequestReloadIgnoringCacheData or NSURLRequestReloadIgnoringLocalAndRemoteCacheData in the request (by the way the resulting image will get re-cached by AFNetworking, so that's one way to force it to refresh it's cache).

Now, sure if the AFImageCache returns no hit, then AFNetworking will (I think), for exactly one request, use Apple's default NSURLCache which in theory does respect http cache headers. But that is just one request, because AFNetworking, then throws the result into its own permanent, non-http aware cache.

Let me know if I've got this wrong or missed anything.

like image 412
Fraggle Avatar asked May 14 '15 10:05

Fraggle


1 Answers

According to Matt, the developer for AFNetworking, the solution to this is to override the behavior by setting the shared image cache to another class that conforms to the AFImageCache protocol. https://github.com/AFNetworking/AFNetworking/issues/2731

like image 83
Fraggle Avatar answered Nov 04 '22 22:11

Fraggle