代码之家  ›  专栏  ›  技术社区  ›  sigvaria

AFNetworking 2.0 AFHTTPRequestOperationManager CachePolicy不工作

  •  0
  • sigvaria  · 技术社区  · 11 年前

    我正在尝试使用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) { ....
    
    1 回复  |  直到 11 年前
        1
  •  4
  •   Rob Md Fahim Faez Abir    11 年前

    这是AFNetworking最新版本中的一个错误,在 issue #2563 1月24日,试图解决一些 other issue ,引入了此错误。要解决这个问题

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

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

    对于这个bug缺乏回应是非常令人失望的。假设这确实是您所面临的问题(请尝试上述方法之一,看看它是否能解决您的问题),请加入我们,并在下面的讨论中发表您自己的评论 第2563期 。越多的人参与进来,问题就越有可能得到解决。


    注:这是在2015年3月26日提交时固定的 7d8e286 .