3

I've configured the session:

- (void)createSession {
    NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration];
    sessionConfig.requestCachePolicy = NSURLRequestReturnCacheDataElseLoad;
    _session = [NSURLSession sessionWithConfiguration:sessionConfig];
}

Then, I try to make requests to the server (getting images from Twitter's API):

    NSURL *url = tweet.mediaUrl;
    NSURLSessionDownloadTask *getImageTask =
    [_session downloadTaskWithURL:url
                completionHandler:^(NSURL *location,
                                    NSURLResponse *response,
                                    NSError *error) {
                    UIImage *downloadedImage = [UIImage imageWithData: [NSData dataWithContentsOfURL:location] ];
                    dispatch_async(dispatch_get_main_queue(), ^{

                    //setting image to a view


                    });
                }];

    [getImageTask resume];

The images are being loaded and set correctly, but when I turn off Internet connection on my machine (without terminating the application, it still runs...) I got empty UIImageViews - so the session tries to download images from server, but does not use any cache.
What does cause this problem?
I tried to use shared session, or to configure shared cache with predefined disk and in-memory space, nothing helped.
iOS Simulator, iOS 8+

Richard Topchii
  • 7,075
  • 8
  • 48
  • 115
  • possible duplicate of [Can I use HTTP caching with an NSURLSessionDownloadTask on iOS?](http://stackoverflow.com/questions/26175133/can-i-use-http-caching-with-an-nsurlsessiondownloadtask-on-ios) – jlehr Jan 02 '15 at 18:46
  • Yes, seems to be like a bug, but I want to see somebody who successfully used the above mentioned technique. – Richard Topchii Jan 03 '15 at 09:28

1 Answers1

0

Cache is different from download task. Downloaded data is stored in temp directory which you need to move to app path if you need permanent. Where as cache data is used (based in response or set cache policy) on next request. NOTE : OS can delete the cache data in some circumstances

mkumar
  • 111
  • 1
  • 10