ios - AFNetworking 2.0 AFHTTPRequestOperationManager CachePolicy 不工作

标签 ios afnetworking nsurlcache

我正在尝试向带有 If-Modified-Since header 的服务器发出 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) { ....

最佳答案

这是最新版本 AFNetworking 中的一个错误,在 issue #2563 中讨论过. 1 月 24 日,尝试解决一些问题 other issue ,引入了这个错误。要解决它,要么

  • 回滚到之前版本的 AFNetworking 2.5.0;或
  • 自己手动发布 KVO 通知:

    [self.requestSerializer willChangeValueForKey:@"cachePolicy"];
    [self.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
    [self.requestSerializer didChangeValueForKey:@"cachePolicy"];
    

关于这个错误缺乏回应是相当令人失望的。假设这确实是您遇到的问题(尝试上述方法之一,看看它是否可以解决您的问题),请加入我们并在 issue #2563 下的讨论中发表您自己的评论。 .对此提出意见的人越多,修复它的可能性就越大。


注意:这已于 2015 年 3 月 26 日在提交 7d8e286 中修复.

关于ios - AFNetworking 2.0 AFHTTPRequestOperationManager CachePolicy 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29052322/

相关文章:

php - AFNetworking 2.0图片上传和JSON返回错误

ios - iOS8 上的简单 NSURLCache 实现

ios10 swift3.0 Alamofire上传图片出现错误

iphone - 如何更改 Root View Controller

ios - 使用 AFNetworking 在异步模式下调整 UIImage 的大小

ios - NSURLCache,连同 NSURLSession,不尊重 : Cache-Control: max-age:86000, private,must-revalidate

ios - 清除 NSURLConnection 缓存

ios - swift 中的 xcode 6 beta EXC_BREAKPOINT(代码=EXC_I386_BPT,子代码=0x0)

php - APNS 不再适用于 PHP

iphone - 将 AFIncrementalStore 与 Auth token 一起使用