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];
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
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];
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With