始终取回NSURLSessionDataTask中的缓存数据 [英] Always getting back cached data in NSURLSessionDataTask
问题描述
使用NSURLSessionDataTask
向服务器发布JSON请求时,我面临一个非常奇怪的问题.
I'm facing a very strange issue when using NSURLSessionDataTask
to post a JSON request to the server.
第一个请求通过,我收到正确的JSON响应,当我执行第二个请求时,我总是返回旧的响应,而服务器从未收到该请求.即使我打开飞行模式,NSURLSessionDataTask
确实可以正常工作,但我又恢复了原来的响应.
The first request goes through and I receive the correct JSON response, when I do a second request I'm getting always back the old response and the server never receives the request. Even if I turn on airplane mode the NSURLSessionDataTask
does work an I get back the old response again.
那是我正在使用的代码:
That's 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];
}
推荐答案
我已经找到了问题,我不知道为什么会有所改变,因为我以前从未设置过该属性.无论如何,我设置了configuration.URLCache = NULL;
,现在一切正常.
I've found the issue, I don't know why this has changed, because I never had to set that property before. Anyway I set configuration.URLCache = NULL;
and now everthing works fine again.
这篇关于始终取回NSURLSessionDataTask中的缓存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!