10

Hi All can any one help me out how to clear the cache in AFNetworking.

I used to have old version of AFNetworking and i see that it has been updated, Can any body help me out how to clean the cache for AFnetworking.

Previously it was something like this

SDImageCache *imageCache = [SDImageCache sharedImageCache];
[imageCache clearMemory];
[imageCache clearDisk];
[imageCache cleanDisk];
Dheeraj Kaveti
  • 171
  • 1
  • 2
  • 10

5 Answers5

16

If you're using AFNetworking, I recently implemented a little workaround to remove the image from the cache.

Look in the file "UIImageView+AFNetworking.h"

Under @interface UIImageView (AFNetworking), add the following:

- (void)clearImageCacheForURL:(NSURL *)url;

And Under @protocol AFImageCache, add:

- (void)clearCachedRequest:(NSURLRequest *)request;

Next, open "UIImageView+AFNetworking.m"

Under @implementation UIImageView (AFNetworking), add the following:

- (void)clearImageCacheForURL:(NSURL *)url {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];

    UIImage *cachedImage = [[[self class] sharedImageCache] cachedImageForRequest:request];
    if (cachedImage) {
        [[[self class] sharedImageCache] clearCachedRequest:request];
    }
}

Almost done! Now just add the following below @implementation AFImageCache:

- (void)clearCachedRequest:(NSURLRequest *)request {
    if (request) {
        [self removeObjectForKey:AFImageCacheKeyFromURLRequest(request)];
    }
}

And you're good to go! Now when you need to clear a particular image url from the cache, just call [imageView clearImageCacheForURL:imgURL];

Community
  • 1
  • 1
bachonk
  • 3,924
  • 1
  • 14
  • 14
  • Hi, I found this workaround does not work for UIButton+AFNetworking, is there any way to remove image cache for UIButton ? – moligaloo Nov 25 '14 at 13:51
  • @bachnok can you please tell me what is imgURL in last line ? – Abha Apr 14 '16 at 09:21
  • 1
    @Abha If you're trying to clear the cache for an image from something like `http://example.com/image.jpg`, then you would call: `[imageView clearImageCacheForURL:[NSURL urlWithString:@"http://example.com/image.jpg"]]` – bachonk Apr 14 '16 at 14:06
6

https://github.com/AFNetworking/AFNetworking/wiki/AFNetworking-FAQ

Does AFNetworking have any caching mechanisms built-in?

AFNetworking takes advantage of the caching functionality already provided by NSURLCache and any of its subclasses.

We recommend you try out Pete Steinberger's fork of SDURLCache, which provides disk caching, which is not otherwise implemented by NSURLCache on iOS. You may find it useful to set ignoreMemoryOnlyStoragePolicy to YES for the SDURLCache instance, which will ensure that images will be cached to disk more consistently for offline use (which, as reported in the SDURLCache README, iOS 5 supports, but only for http, so it still remains a good option if you want to support iOS 4 or https)

I would also see this question: How To Disable AFNetworking Cache

Also this: AFNetworking and caching

The moment you activate the NSURLCache you can specify it's maximum size. You can also clear the cache by calling the removeAllCachedResponses or removeCachedResponsesForRequest methods.

Hope it helps.

Community
  • 1
  • 1
StuartM
  • 6,743
  • 18
  • 84
  • 160
4

If you are using AFNetworking using cocoaPods then you can do this by making a category of UIImageView (clearCache)

        - (void)clearImageCacheForURL:(NSURL *)url {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"];

UIImage *cachedImage = [[[self class] sharedImageCache] cachedImageForRequest:request];
if (cachedImage) {
    [self clearCached:[[self class] sharedImageCache] Request:request];
}
}

- (void)clearCached:(NSCache *)imageCache Request:(NSURLRequest *)request {
if (request) {
    [imageCache removeObjectForKey:[[request URL] absoluteString]];
}
}
Umair Aamir
  • 1,624
  • 16
  • 26
3

If you are using AFNetworking3.0 through cocoapods, then use below code by creating category for UIImageView.

#import <AFNetworking/UIImageView+AFNetworking.h>
#import <AFNetworking/AFImageDownloader.h>
#import <AFNetworking/AFAutoPurgingImageCache.h>

+ (void)clearImageCacheForURL:(NSURL *)url {
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
    id <AFImageRequestCache> imageCache = [[self class] sharedImageDownloader].imageCache;
    UIImage *cachedImage = [imageCache imageforRequest:request withAdditionalIdentifier:nil];
    if (cachedImage) {
        [self clearCached:imageCache Request:request];
    }
}

+ (void)clearCached:(AFAutoPurgingImageCache *)imageCache Request:(NSURLRequest *)request {
    if (request) {
        [imageCache removeImageforRequest:request withAdditionalIdentifier:nil];
    }
}
Anjaneyulu Battula
  • 1,910
  • 16
  • 33
1
AFImageDownloader *imageDownloader = [AFImageDownloader   defaultInstance];

NSURLCache *urlCache = imageDownloader.sessionManager.session.configuration.URLCache;

[urlCache removeAllCachedResponses];

[imageDownloader.imageCache removeImageWithIdentifier:url];

this will help to remove image in cash. url is the image url that needs to remove