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+