Ios NSURLSession (default config) does not cache requests

I set up a session:

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

      

Then I try to make requests to the server (fetching images from the Twitter 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 loading and installing correctly, but when I turn off the internet connection on my machine (without terminating the application, it still works ...) I got empty UIImageViews - so the session tries to load images from the server but doesn't use the cache. What is causing this problem?
I tried using a shared session or setting up a shared cache with a predefined disk and in memory, nothing helped.
iOS Simulator, iOS 8+

+3


source to share


1 answer


The cache is different from the download task. The downloaded data is stored in the temp directory, which you need to move to the application path if you need persistent. Where is the cache data (based on response policy or cache set) used on the next request. NOTE. OS can delete cache data in some cases.



0


source







All Articles