对于每个NSURLRequest来讲都对应着一个响应的 NSURLRequestCachePolicy 缓存策略,缓存策略分别为:

typedef NS_ENUM(NSUInteger, NSURLRequestCachePolicy)
{
    NSURLRequestUseProtocolCachePolicy = 0,

    NSURLRequestReloadIgnoringLocalCacheData = 1,
    NSURLRequestReloadIgnoringLocalAndRemoteCacheData = 4, // Unimplemented
    NSURLRequestReloadIgnoringCacheData = NSURLRequestReloadIgnoringLocalCacheData,

    NSURLRequestReturnCacheDataElseLoad = 2,
    NSURLRequestReturnCacheDataDontLoad = 3,

    NSURLRequestReloadRevalidatingCacheData = 5, // Unimplemented
};

默认情况下AFN的缓存策略采用 NSURLRequestUseProtocolCachePolicy,即如果一个NSCachedURLResponse对于请求并不存在,数据将会从源端获取。如果请求拥有一个缓存的响应,那么URL加载系统会检查这个响应来决定,如果它指定内容必须重新生效的话。假如内容必须重新生效,将建立一个连向源端的连接来查看内容是否发生变化。假如内容没有变化,那么响应就从本地缓存返回数据。如果内容变化了,那么数据将从源端获取。

在请求以及响应都正常的情况下,不会发生问题,当请求报错的时候AFN对于同一个URL敬请访问的时候,会直接取之前缓存的响应结果,结果就是当服务器数据正常的情况下也会返回之前报错的时候的响应,这显然会造成问题!
AFN本身是通过系统的NSURLCache来做缓存的,所以为了解决问题,就是在网络请求发生错误的时候调用即可,
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:@"xxxx"];

  1. I wish to get across my gratitude for your generosity supporting those who really need help with this important area. Your real dedication to passing the message all around was unbelievably helpful and has surely enabled associates much like me to attain their desired goals. This insightful advice can mean much to me and much more to my colleagues. Regards; from all of us.

  2. A lot of thanks for every one of your hard work on this web page. Debby loves managing investigation and it’s simple to grasp why. We all know all concerning the dynamic manner you provide useful suggestions via the blog and in addition recommend contribution from visitors on this area of interest and my simple princess is without question discovering a lot. Take advantage of the rest of the year. You are doing a first class job.

  3. I in addition to my friends have been digesting the great ideas found on your website and then suddenly developed a terrible suspicion I had not thanked the website owner for them. These guys became as a result thrilled to see all of them and already have seriously been making the most of them. We appreciate you indeed being very accommodating as well as for pick out such brilliant information most people are really needing to be aware of. Our own sincere regret for not expressing appreciation to earlier.

  4. I wish to convey my appreciation for your kind-heartedness giving support to men and women who really need help with this one content. Your personal dedication to getting the solution up and down appeared to be pretty important and have regularly helped somebody like me to attain their desired goals. The insightful information means a great deal to me and much more to my office colleagues. Many thanks; from everyone of us.

  5. I truly wanted to type a brief word so as to thank you for all the splendid advice you are giving out at this site. My rather long internet research has finally been recognized with really good know-how to share with my good friends. I would repeat that most of us visitors actually are quite blessed to live in a magnificent site with so many marvellous people with valuable techniques. I feel extremely grateful to have discovered the web page and look forward to some more enjoyable minutes reading here. Thank you once more for a lot of things.

  6. I have been surfing on-line more than 3 hours today, but I by no means discovered any fascinating article like yours.
    It is pretty value sufficient for me. In my view, if all web owners and
    bloggers made good content as you did, the internet will be
    a lot more helpful than ever before.

Leave a Reply

电子邮件地址不会被公开。

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>