之前做项目遇到这么一个问题:网络正常的情况下,如果服务器宕机或者数据库出错,会造成访问服务器报错的情况,一般报错的内容是:无法连接到服务器或者其它错误。且服务器修复后,仍然报错。经过排查,终于找出了原因所在:AFNetworking会将Url的Response缓存,方便离线浏览。而且这是默认 存在的
- (void)setCacheResponseBlock:(NSCachedURLResponse * (^)(NSURLConnection *connection, NSCachedURLResponse *cachedResponse))block {
self.cacheResponse = block;}
我们来看AFNetworking的方法说明:
@param block A block object to be executed to determine what response a connection will cache, if any. The block returns an NSCachedURLResponse
object, the cached response to store in memory or nil
to prevent the response from being cached, and takes two arguments: the URL connection object, and the cached response provided for the request.,问题就出在这,一旦Response被缓存后,下次不再重复发起连接,将直接将同样地Response返回,也就是说:网络正常的情况下,服务器出错, 一旦服务器出错的Response被缓存,就算服务器紧急修复后,也有可能造成移动客户端持续报错。这种情况很难发现。
我们再来看苹果是
1、An NSCachedURLResponse object encapsulates an NSURLResponse object, an NSData object containing the content corresponding to the response, and an NSDictionary containing application specific information.The NSURLCache system stores and retrieves instances of NSCachedURLResponse.这句话简单介绍了一下NSCachedURLResponse的构成,以及被NSURLCache来存储和读取。最重要的在下面:
2、我们来看下系统提供的URLCache缓存策略:
NSURLCacheStoragePolicyThese constants specify the caching strategy used by an NSCachedURLResponse object.typedef enum{ NSURLCacheStorageAllowed, NSURLCacheStorageAllowedInMemoryOnly, NSURLCacheStorageNotAllowed,} NSURLCacheStoragePolicy;
我们依次来解读:
NSURLCacheStorageAllowed:Specifies that storage in NSURLCache is allowed without restriction.
NSURLCacheStorageAllowedInMemoryOnly:
Specifies that storage in NSURLCache is allowed; however storage should be restricted to memory only.
这个不用过多解释,缓存在内存:
NSURLCacheStorageNotAllowed
Specifies that storage in NSURLCache is not allowed in any fashion, either in memory or on disk.
不允许任何协议的缓存,即不允许缓存。
解决方案:
幸运的是,AFNetworking利用的系统自有类存储,我们可以修改其源代码:
[[NSURLCache sharedURLCache] removeAllCachedResponses];
这一句代码是清除所有的URL缓存Response。这样一来,就可以解决这一问题所在。
PS:我觉得可以根据业务修改代码逻辑,做到报错的响应不缓存,这样才比较合理。单纯清空全部缓存或者不启用缓存的话就失去缓存的意义了。
ref:http://blog.csdn.net/wojiaoliuli521/article/details/19831077