AFN的坑--NSCachedURLResponse缓存

网络正常的情况下,如果服务器宕机或者数据库出错,会造成访问服务器报错的情况,一般报错的内容是:无法连接到服务器或者其它错误。且服务器 修复后,仍然报错。经过排查,终于找出了原因所在:AFNetworking会将Url的Response缓存,方便离线浏览。而且这是默认存在的,无论 是1.x还是2.x版本都存在。其方法是:

-(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被缓存,就算服务器紧急修复后,也有可能造成iOS客户端持续报错。这种情况将是致命的。

来看苹果是怎么说的:

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缓存策略:

NSURLCacheStoragePolicy

These 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.

没有特殊指定时采取该策略。

Important: iOS prior to version 5 ignores this cache policy, and instead treats it as NSURLCacheStorageAllowedInMemoryOnly.

 

重要:iOS在5之前都是忽略这个协议的,代替的是NSURLCacheStorageAllowedInMemoryOnly。(也就是说,6之后这个协议是默认协议,既可以缓存在内存,也可以缓存在磁盘。如果是内存,我们重启设备就可以解决,如果是磁盘,必须硬代码去解决)

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。这样一来,就可以解决这一问题所在。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值