In our application we use AFNetorking through Cocapods (currently version 3.1.0) for image downloading, and we use disk caching, it is working but it seems like there is no limit in the size of the cache on disk and I don't know how to set one.
Doc says:
AFNetworking takes advantage of the caching functionality already provided by NSURLCache and any of its subclasses. So long as your NSURLRequest objects have the correct cache policy, and your server response contains a valid Cache-Control header, responses will be automatically cached for subsequent requests.
Our image responses have the cache-control header as this:
Cache-Control: public, max-age=31209907
And we are using this code to get the images (setting the cache policy explicitly):
- (void)setImageWithURLString:(NSString *)URLString success:(void (^)())success
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
[request addValue:@"image/*" forHTTPHeaderField:@"Accept"];
[request addValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];
__weak __typeof(self)weakSelf = self;
[self setImageWithURLRequest:request placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
if (strongSelf && success) {
strongSelf.image = image;
success();
}
} failure:nil];
}
Disk capacity is set to 150 MB by default (as I see in AFNetworking):
+ (NSURLCache *)defaultURLCache {
return [[NSURLCache alloc] initWithMemoryCapacity:20 * 1024 * 1024
diskCapacity:150 * 1024 * 1024
diskPath:@"com.alamofire.imagedownloader"];
}
And I'm trying to set it to 100 MB like this:
AFImageDownloader *imageDownloader = [AFImageDownloader defaultInstance];
NSURLCache *urlCache = imageDownloader.sessionManager.session.configuration.URLCache;
urlCache.diskCapacity = 100 * 1024 * 1024;
If I let the default value of 150 MB, the capacity is ignored. Also if I set the disk capacity to 100 MB, the value is ignored.
I can confirm that by accessing currentDiskUsage
. It goes, in every case, far beyond the diskCapacity
.
I have 2 questions basically:
- Am I missing something in my setup?
- Is it possible to set the disk capacity so that the cache has a limit?
Thanks in advance :)