Alex L
Alex L

Reputation: 8449

Images are not removed from disk after clearing cache AFNetworking

I tried to clear cache bit the size of the app does not become significantly smaller.

My app size is about 30MB plus 45MB for cached images.

This is how I attempted to clear cache:

 [[NSURLCache sharedURLCache] removeAllCachedResponses];
 [[AFImageDownloader defaultURLCache] removeAllCachedResponses];
 AFAutoPurgingImageCache *imageCache = [AFImageDownloader defaultInstance].imageCache;
 [imageCache removeAllImages];            

This is how I download images using AFNetworking (3.1.0):

#import "AFImageDownloader.h"

[photoImageView setImageWithURLRequest:[NSURLRequest requestWithURL:thumb] placeholderImage:myCachedImage success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
                [self setPhotoImage:image];
            } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
                [self setupPlaceholder];
            }];

Upvotes: 4

Views: 751

Answers (3)

Maxim
Maxim

Reputation: 125

This code works for AFNetworking 3.1:

        [[NSURLCache sharedURLCache] removeAllCachedResponses];

        AFImageDownloader *imageDownloader = [AFImageDownloader defaultInstance];
        NSURLCache *urlCache = imageDownloader.sessionManager.session.configuration.URLCache;
        [urlCache removeAllCachedResponses];

        [imageDownloader.imageCache removeAllImages];

Upvotes: 0

Himanshu Moradiya
Himanshu Moradiya

Reputation: 4815

We are seeing this in one of our apps as well. According to this http://blog.airsource.co.uk/2014/10/11/nsurlcache-ios8-broken/, its an iOS 8 bug which Apple will have corrected in the iOS 8.1 update today.

So to answer your question: No, you shouldn't have to delete the cache yourself and with some luck Apple's fix really will clean up the old data. If for some reason not, you should be able to delete it yourself without any problems.

I've verified that iOS 8.1 does indeed correct the issue. The user must launch the app once after update for the cache to be cleared however.

Upvotes: 1

jhd
jhd

Reputation: 1253

See follows:

  1. AFAutoPurgingImageCache *imageCache = [AFImageDownloader defaultInstance].imageCache; The imageCache is stored in the memory, it is useless to do [imageCache removeAllImages];
  2. You just need to do [[AFImageDownloader defaultURLCache] removeAllCachedResponses];. It is the only API that apple offers to clear the URLCache.
  3. You find the fsCachedData is not removed when you call removeAllCachedResponses. It is because apple has its own mechanism to clear the disk data. Maybe it will clear the disk data in every 7 days as i searched, but i can not make sure.
  4. I think it is interesting to see how the others use fsCachedData. Should I clear the fsCachedData folder myself?
  5. I hope it will help u.

Upvotes: 2

Related Questions