1
votes

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:

  1. Am I missing something in my setup?
  2. Is it possible to set the disk capacity so that the cache has a limit?

Thanks in advance :)

1

1 Answers

0
votes

As of AFNetworking 3.1:

AFImageDownloader -init creates AFHTTPSessionManager using AFImageDownloader +defaultURLSessionConfiguration. Internally, AFHTTPSessionManager (and AFURLSessionManager) creates its own NSURLSession using AFImageDownloader +defaultURLSessionConfiguration which is already configured to use AFImageDownloader +defaultURLCache with hardcoded 20/150.

See: https://developer.apple.com/documentation/foundation/nsurlsessionconfiguration?language=objc

UPDATE 2018-01-17:

As of AFNetworking 3.2.0:

One may configure AFImageDownloader's NSURLSessionConfiguration before the NSURLSession is created:

AFImageDownloader with only disk NSURLCache

// Force AFNetworking to NOT use any RAM whatsoever for image downloading
// (neither backed by NSURLCache nor imageCache). We will solely rely on
// NSURLCache backed by disk
// (i.e. won't use AFAutoPurgingImageCache and NSURLCache.memoryCapacity=0 disk=300)
NSURLCache* imageDownloaderCache = [[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:300 * 1024 * 1024 diskPath:@"com.alamofire.imagedownloader"];

NSURLSessionConfiguration* imageDownloaderSessionConfiguration = [AFImageDownloader defaultURLSessionConfiguration];
imageDownloaderSessionConfiguration.URLCache = imageDownloaderCache;
AFImageDownloader* imageDownloader = [[AFImageDownloader alloc] initWithSessionConfiguration:imageDownloaderSessionConfiguration];
imageDownloader.imageCache = nil;

// If in the future, we wanted to cache thumbnails in RAM, our own P2AFAutoPurgingImageCache shall do the trick
//    imageDownloader.imageCache = [[P2AFAutoPurgingImageCache alloc] initWithMemoryCapacity:5 * 1024 * 1024 preferredMemoryCapacity:4 * 1024 * 1024];

[UIImageView setSharedImageDownloader:imageDownloader];

Optional P2AFAutoPurgingImageCache that only caches thumbnails

@interface P2AFAutoPurgingImageCache : AFAutoPurgingImageCache
@end

@implementation P2AFAutoPurgingImageCache

-(BOOL)shouldCacheImage:(UIImage *)image forRequest:(NSURLRequest *)request withAdditionalIdentifier:(NSString *)identifier
{
    // Only cache thumbnails
    return image.size.width <= 200 && image.size.height <= 200;
}

@end

Details here: https://github.com/AFNetworking/AFNetworking/pull/4010