AFNetworking:如何知道响应是否正在使用缓存? 304或200
我找不到任何问题的答案,可能是我错过了什么……
当我要求提供url时,我需要知道响应是来自缓存还是来自网络。
状态代码是304还是200? (但AFNetworking
总是回应200)
使用ASIHTTPRequest
我曾经从ASIHTTPRequest
检查“ didUseCachedResponse
”,这是完美的。
我想我找到了一个解决方案来确定是否使用AFNetworking 2.0从缓存返回响应。 我发现每次从服务器(状态200,而不是304)返回新响应时, AFHTTPRequestOperation
调用cacheResponseBlock
,它是cacheResponseBlock
的属性。 如果响应应缓存,则块应返回NSCachedURLResponse
否则返回nil。 这样您就可以过滤响应并仅缓存部分响应。 在这种情况下,我正在缓存来自服务器的所有响应。 诀窍是,当服务器发送304并从缓存加载响应时,将不会调用此块。 所以,这是我正在使用的代码:
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; }];
这个解决方案对我有用,到目前为止我还没有发现任何问题。 但是,如果您对我的解决方案有更好的想法或反对意见,请随时发表评论!
似乎苹果不想让你知道它是否来自缓存。
我通过保存修改日期与请求相关联找到了一种方法,并且在AFNetWorking回答我时比较了这个日期。
不像我想的那么干净,但有效……
有一种方法可以指定在AFNetworking中应该被视为成功的状态代码,它是通过响应序列化来完成的,这里是代码
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; AFHTTPResponseSerializer *respSerializer = [AFHTTPResponseSerializer serializer]; NSMutableIndexSet *responseCodes = [NSMutableIndexSet indexSet]; [responseCodes addIndex:200]; [responseCodes addIndex:304]; [operation setResponseSerializer:respSerializer];
使用此代码,AFNetworking将304视为成功