AFNetworking:如何知道响应是否使用缓存?304 或 200 [英] AFNetworking : How to know if response is using cache or not ? 304 or 200

查看:25
本文介绍了AFNetworking:如何知道响应是否使用缓存?304 或 200的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到我的问题的任何答案,可能是我错过了什么......

I can't find any answer to my question, may be I miss something...

当我请求一个 url 时,我需要知道响应是来自缓存还是来自网络.

When I ask for a url, I need to know if the response comes from the cache or from the net.

状态码是 304 还是 200 ?(但 AFNetworking 总是响应 200)

Is the status code 304 or 200 ? (but AFNetworking always responde 200)

使用 ASIHTTPRequest 我曾经从 ASIHTTPRequest 中检查didUseCachedResponse",这很完美.

With ASIHTTPRequest I used to check "didUseCachedResponse" from ASIHTTPRequest, this was perfect.

推荐答案

我想我找到了一个解决方案来确定响应是从缓存返回还是不使用 AFNetworking 2.0.我发现每次从服务器返回新响应(状态 200,而不是 304)时,都会调用 cacheResponseBlock,它是 AFHTTPRequestOperation 的一个属性.如果响应应该被缓存,块应该返回 NSCachedURLResponse 或者如果它不应该返回 nil.这样您就可以过滤响应并仅缓存其中的一部分.在这种情况下,我正在缓存来自服务器的所有响应.诀窍是,当服务器发送 304 并从缓存加载响应时,不会调用此块.所以,这是我正在使用的代码:

I think I found a solution to determine if response was returned from cache or not using AFNetworking 2.0. I found out that each time a new response is returned from the server (status 200, not 304) the cacheResponseBlock which is a property of AFHTTPRequestOperation is called. The block should return NSCachedURLResponse if response should be cached or nil if it shouldn't. That's way you can filter responses and cache only some of them. In this case, I am caching all responses that comes from the server. The trick is, that when server sends 304 and response is loaded from cache, this block won't be called. So, this is the code I am using:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

BOOL __block responseFromCache = YES; // yes by default

void (^requestSuccessBlock)(AFHTTPRequestOperation *operation, id responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject) {
    if (responseFromCache) {
        // response was returned from cache
        NSLog(@"RESPONSE FROM CACHE: %@", responseObject);
    }
    else {
        // response was returned from the server, not from cache
        NSLog(@"RESPONSE: %@", responseObject);
    }
};

void (^requestFailureBlock)(AFHTTPRequestOperation *operation, NSError *error) = ^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"ERROR: %@", error);
};

AFHTTPRequestOperation *operation = [manager GET:@"http://example.com/"
                                      parameters:nil
                                         success:requestSuccessBlock
                                         failure:requestFailureBlock];

[operation setCacheResponseBlock:^NSCachedURLResponse *(NSURLConnection *connection, NSCachedURLResponse *cachedResponse) {
    // this will be called whenever server returns status code 200, not 304
    responseFromCache = NO;
    return cachedResponse;
}];

此解决方案对我有用,到目前为止我还没有发现任何问题.但是,如果您对我的解决方案有更好的想法或反对意见,请随时发表评论!

This solution works for me and I haven't found any issues so far. But, if you have a better idea or some objections against my solution, feel free to comment!

这篇关于AFNetworking:如何知道响应是否使用缓存?304 或 200的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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