0

I am using AFNetworking version 2.5. I described my problem below. Thanks in advance.

My problem:

When I debug, I see AFNetworking downloads image and stores it to disk. After restart, cache for image remains in disk, however it does not return from NSCache.

Please note that app is also not receiving any memory warning, nor clears cache automatically. Each time I restart, I check files from finder and see they all remain, but AFNetworking can not receive it from cache, and downloads it again.

Below line of "UIImageView+AFNetworking.m" returns nil:

return [self objectForKey:AFImageCacheKeyFromURLRequest(request)];

My Code:

NSURL *imageURL = [NSURL URLWithString:facility.image.list.original];
NSMutableURLRequest *imageURLRequest = [NSMutableURLRequest requestWithURL:imageURL];
[imageURLRequest addValue:@"image/*" forHTTPHeaderField:@"Accept"];
[cell.image setImageWithURLRequest:imageURLRequest placeholderImage:nil success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) {
            if (request)
            {
                [UIView transitionWithView:cell.image
                                  duration:0.2f
                                   options:UIViewAnimationOptionTransitionCrossDissolve
                                animations:^{[cell.image setImage:image];}
                                completion:NULL];
            }
            else
            {
                [cell.image setImageWithURL:imageURL];
            }
        } failure:nil];
Berk
  • 1,289
  • 12
  • 16

1 Answers1

1

Please refer to AFImageCache description.

"AFImageCache: a memory-only image cache private to AFNetworking, subclassed off of NSCache"

If you need to cache your image on disc you can use other libraries like SDWebImageCache.

Nat
  • 12,032
  • 9
  • 56
  • 103
  • Code line that I provided belongs to an AFNetworking class that is extended from NSCache, but NSCache is also using disk. So it is not providing a memory only caching. As I said, I can see disk cached images. I think there is a problem with disk caching parameters that are passed to NSCache, I want to understand if it can be fixed by either overriding NSCache default parameters, or modifying response header for images. Also, I implemented a workaround that loads cached images from disk when launch finished, to make above line of AFNetworkig code returns image data. – Berk Sep 11 '15 at 14:23
  • @Berk NSCache is not totally stable. I don't think AFNetworking should be considered as caching library. Please check out this issue: https://github.com/AFNetworking/AFNetworking/issues/2131. Also there are many issues eg like this: https://github.com/AFNetworking/AFNetworking/issues/2784. I really recommend using dedicated solution instead of this. AFNetworking is a great library, but it's focused mainly on different aspects than image caching. Besides, you also have cache speed. Check out this benchmark: https://bpoplauschi.wordpress.com/2014/03/21/ios-image-caching-sdwebimage-vs-fastimage/ – Nat Sep 14 '15 at 07:17
  • @Berk Here is also great article (btw by creator of AFNetworking) about NSCache: http://nshipster.com/nscache/. It's quite worth reading in this context. – Nat Sep 14 '15 at 07:21
  • Thanks for help Vive. I will read articles asap. But I still think that NSCache is using data from response header, and there is information that we lack to tune caching behaviour. Also using NSCache has advantages, since it uses private api's to increase overall device perfomance. Thus, even I use custom cache mechanism, it can bother user. I will try to stick with NSCache with my current workaround. Thanks anyways ;) – Berk Sep 14 '15 at 08:46