AFNetworking 2.0 AFHTTPRequestOperationManager CachePolicy无法正常工作 [英] AFNetworking 2.0 AFHTTPRequestOperationManager CachePolicy not working

查看:103
本文介绍了AFNetworking 2.0 AFHTTPRequestOperationManager CachePolicy无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试使用If-Modified-Since标头向服务器发出GET请求,以避免加载相同的内容。当我发出请求时,我知道服务器正在发送回304,指示内容没有更改,但是NSURLCache或AFNetworking用200响应了缓存的内容。为了防止这种情况,我将请求序列化程序的缓存策略设置为NSURLRequestReloadIgnoringLocalCacheData,但这不能解决问题。

I'm trying to make a GET request to a server with an If-Modified-Since header in order to avoid loading identical content. When I make a request I know that the server is sending back a 304 indicating that the content has not changed, but NSURLCache or AFNetworking is responding with a 200 with the cached content. In order to prevent this I set the request serializer's cache policy to NSURLRequestReloadIgnoringLocalCacheData, however this does not solve the problem.

- (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中的错误,在问题#2563 。 1月24日,尝试解决某些其他问题,此错误被引入。要解决该问题,可以

This is a bug in the latest version of AFNetworking, discussed in issue #2563. On January 24th, an attempt to resolve some other issue, this bug was introduced. To resolve it, either


  • 回滚到AFNetworking 2.5.0的先前版本;或

  • 自己手动发布KVO通知:

  • roll back to a previous version of AFNetworking 2.5.0; or
  • manually post the KVO notifications yourself:

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


对此错误的响应不足令人失望。假设这确实是您所遇到的问题(尝试上述方法之一,看看是否可以解决您的问题),请加入我们,并在问题#2563

The lack of response regarding this bug is quite disappointing. Assuming this is indeed the issue you're suffering from (try one of the above and see if it remedies your problem), please join us and post your own comment in the discussion under issue #2563. The more people chime in on this, the more likely it is to be fixed.

注意:该问题已在3月26日修复2015年提交 7d8e286

Note: This was fixed 26 March 2015 in commit 7d8e286.

这篇关于AFNetworking 2.0 AFHTTPRequestOperationManager CachePolicy无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆