我正在尝试向具有If-Modified-Since标头的服务器发出GET请求,以避免加载相同的内容。当我发出请求时,我知道服务器正在发回304表示内容没有改变,但是NSURLCache或AFNetworking正在响应带有缓存内容的200。为了防止这种情况,我将请求序列化程序的缓存策略设置为NSURLRequestReloadIgnoringLocalCacheData,但这并不能解决问题。
- (void)getConfig {
//Retrive the timeStamp at which the config was last updated
NSDate *timeStamp = [[NSUserDefaults standardUserDefaults] objectForKey:@"lastGetConfigDate"];
//Get the rfc1123 representation of the timeStamp
NSString *dateString = [timeStamp rfc1123String];
//If we're not force loading the data then include the time stamp in the If-modified-Since header
[self.requestSerializer setValue:[NSString stringWithFormat:@"%@", dateString] forHTTPHeaderField:@"If-Modified-Since"];
//Setting cachePolicy to ignore cache data to prevent the cached response
[self.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
//GET REQUEST to /api/config
[self GET:@"/api/config" parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *jsonDict = (NSDictionary *)responseObject;
DDLogInfo(@"Config: 200");
//If the config data was successfully loaded then set the lastGetConfigDate time stamp in the nsuserdefaults to send in the next call with the if-modified-since header
if (success) {
[[NSUserDefaults standardUserDefaults] setObject:[NSDate date] forKey:@"lastGetConfigDate"];
}
} failure: ^(AFHTTPRequestOperation *operation, NSError *error) { ....
答案 0 :(得分:4)
这是最新版AFNetworking中的一个错误,在issue #2563中讨论过。 1月24日,试图解决一些other issue,这个错误被引入。要解决它,
自行手动发布KVO通知:
[self.requestSerializer willChangeValueForKey:@"cachePolicy"];
[self.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[self.requestSerializer didChangeValueForKey:@"cachePolicy"];
对这个错误缺乏回应是非常令人失望的。假设这确实是您遇到的问题(尝试上述之一并查看它是否可以解决您的问题),请加入我们并在issue #2563下的讨论中发表您自己的评论。人们越多,就越有可能被修复。
注意:这已于2015年3月26日在提交7d8e286中修复。