Always get cached data in NSURLSessionDataTask

I had a very strange problem when using NSURLSessionDataTaskJSON to send a server request.

The first request passes, and I get the correct JSON response, when I execute the second request, I always return the old answer, and the server never receives the request. Even if I turn on airplane mode, it NSURLSessionDataTaskworks, and I again return the old answer.

What is the code I'm using:

- (void)getJSONFromURL:(NSURL*)url identifierCode:(NSInteger)code
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request addValue:[DataController sharedInstance].currentUser.userToken forHTTPHeaderField:@"User-Token"];
[request setHTTPMethod:@"GET"];

if (![SVProgressHUD isVisible])
    [SVProgressHUD show];

NSURLSessionDataTask *postTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error){
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];

    [self handleResponse:httpResponse withJSON:json identifierCode:code];

    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
    });
}];

[postTask resume];

}

+4
source share
2 answers

, , , . , configuration.URLCache = NULL;, .

+6

request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;

+1

Source: https://habr.com/ru/post/1540702/


All Articles