Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSURLSession cache does not work in iOS8

I have following code, it works fine in iOS7, but does not in iOS8.In iOS8, the app always use the cache even I have set NSURLRequestReloadIgnoringCacheData:

NSURL *url = [NSURL URLWithString:k_Chapter_URL];
NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url];

NSURLSessionConfiguration *sessionConfig =
[NSURLSessionConfiguration defaultSessionConfiguration];
sessionConfig.requestCachePolicy = NSURLRequestReloadIgnoringCacheData;
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig];

[[session dataTaskWithRequest:request
            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

            }] resume];

Is there anybody have similar situation with me?

like image 755
ikzjfr0 Avatar asked Nov 10 '22 03:11

ikzjfr0


1 Answers

I think this is a bug in iOS 8. I was able to use resetWithCompletionHandler to force the cache to clear.

[session resetWithCompletionHandler:^{
    [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        }] resume];
}];
like image 181
Eric Y Avatar answered Dec 26 '22 15:12

Eric Y