user139816
user139816

Reputation: 214

AFNetworking 2 How to stop caching?

I am tearing my hair out. The following code refuses to overwrite the previously downloaded file. Any ideas?

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;


AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];


NSURL *URL = [NSURL URLWithString:@"http://www.google.com/images/srpr/logo11w.png"];

NSURLRequest *request = [NSURLRequest requestWithURL:URL
                                         cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                     timeoutInterval:60.0];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {

    NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
    return [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
    NSLog(@"File downloaded to: %@", filePath);
    NSLog(@"error %@", error );
       NSLog(@"response %@", response );

}];
[downloadTask resume];

Upvotes: 1

Views: 1187

Answers (1)

Renato Nascimento
Renato Nascimento

Reputation: 21

Use the ephemeral session configuration instead:

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];

Upvotes: 2

Related Questions