【发布时间】:2015-03-27 09:25:39
【问题描述】:
我正在尝试使用 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) { ....
【问题讨论】:
标签: ios afnetworking nsurlcache