How Does Caching Work in AFNetworking? : AFImageCache & NSUrlCache Explained
来源:互联网 发布:高分一号数据波段 编辑:程序博客网 时间:2024/05/17 21:39
If you are an iOS developer using Mattt Thompson’s ‘delightful networking framework’ AFNetworking
(and if you aren’t, what are you waiting for?), perhaps you have been been curious or confused about the caching mechanism employed and how you can tweak it to your advantage.
AFNetworking
actually takes advantage of 2 separate caching mechanisms:
AFImagecache: a memory-only image cache private to
AFNetworking
, subclassed off ofNSCache
NSURLCache:
NSURLConnection's
default URL caching mechanism, used to storeNSURLResponse
objects : an in-memory cache by default, configurable as an on-disk persistent cache
In order to understand how each caching system works, let’s look at how they are defined:
How AFImageCache Works
AFImageCache
is a part of the UIImageView+AFNetworking
category. It is a subclass of NSCache
, storing UIImage
objects with a URL string as its key (obtained from an input NSURLRequest
object).
AFImageCache
definition:
AFImageCache
is a private implementation of NSCache
. There is no customization that you can do outside of editing the implementation in the the UIImageView+AFNetworking
category, directly. It stores all accessed UIImage
objects into its NSCache. The NSCache
controls when the UIImage
objects are released. If you wish to observe when images are released, you can implement NSCacheDelegate
’s cache:willEvictObject
method.
Edit (03.14.14) : Mattt Thompson has gratiously informed me that as of AFNetworking
2.1, AFImageCache
is configurable. There is now a public setSharedImageCache method. Here’s the full AFN 2.2.1 UIImageView+AFNetworking specification.
How NSURLCache Works
Since AFNetworking
uses NSURLConnection
, it takes advantage of its native caching mechanism, NSURLCache
. NSURLCache
caches NSURLResponse
objects returned by server calls via NSURLConnection
.
Enabled by Default, but Needs a Hand
An NSURLCache
sharedCache
is enabled by default and will be used by any NSURLConnection
objects fetching URL contents for you.
Unfortunately, it has a tendency to hog memory and does not write to disk in its default configuration. To tame the beast and potentially add some persistance, you can simply declare a shared NSURLCache
in your app delegate like so:
Here we declare a shared NSURLCache
with 2mb of memory and 100mb of disk space
Setting the Cache Policy on NSURLRequest Objects
NSURLCache
will respect the caching policy (NSURLRequestCachePolicy
) of each NSURLRequest
object. The policies are defined as follows :
NSURLRequestUseProtocolCachePolicy: specifies that the caching logic defined in the protocol implementation, if any, is used for a particular URL load request. This is the default policy for URL load requests
NSURLRequestReloadIgnoringLocalCacheData: ignore the local cache, reload from source
NSURLRequestReloadIgnoringLocalAndRemoteCacheData: ignore local & remote caches, reload from source
NSURLRequestReturnCacheDataElseLoad: load from cache, else go to source.
NSURLRequestReturnCacheDataDontLoad: offline mode, load cache data regardless of expiration, do not go to source
NSURLRequestReloadRevalidatingCacheData: existing cache data may be used provided the origin source confirms its validity, otherwise the URL is loaded from the origin source.
Caching to Disk with NSURLCache
Cache-Control HTTP Header
The Cache-Control
header MUST be in the HTTP response header from the server in order for the client to cache it. This is a huge gotcha to watch out for. Cache Control
can have parameters defined such as max-age
(how long to cache before updating response), public / private access, or no-cache
(don’t cache response).
Subclass NSURLCache for Ultimate Control
If you would like to bypass the requirement for a Cache-Control
HTTP header and want to define your own rules for writing and reading the NSURLCache
given an NSURLResponse
object, you can subclass NSURLCache
.
Here is an example that uses a CACHE_EXPIRES
value to judge how long to hold on to the cached response before going back to the source:
(Thanks to Mattt Thompson for the feedback and code edits!)
Now that you have your NSURLCache
subclass, don’t forget to initialize it in your AppDelegate in order to use it :
Overriding the NSURLResponse before caching
The -connection:willCacheResponse
delegate is a place to intercept and edit the NSURLCachedResponse
object created by NSURLConnection
before it is cached. In order to edit the NSURLCachedResponse
, return an edited mutable copy as follows (code from NSHipster blog):
Disabling NSURLCache
Don’t want to use the NSURLCache
? Not Impressed? That’s okay. To disable the NSURLCache
, simply zero out memory and disk space in the shared NSURLCache
definition in your appDelegate:
Summary
I wanted to write this blog post for the benefit of the iOS community, to summarize all of the information I found dealing with caching releated to AFNetworking
. We had an internal app loading a lot of images that had some memory issues and performance problems. I was tasked with trying to diagnose the caching behavior of the app. During this exercise, I discovered the information on this post through scouring the web and doing plenty of debugging and logging. It is my hope that this post summarizes my findings and provides an opportunity for others with AFNetworking
experience to add additional information. I hope that you have found this helpful.
- How Does Caching Work in AFNetworking? : AFImageCache & NSUrlCache Explained
- How Does Caching Work in AFNetworking? : AFImageCache & NSUrlCache Explained
- How does a HashMap work in JAVA
- 11、缓存在AFNetworking中是如何工作的?AFImageCache和NSUrlCache给你答案
- AFNetworking中的缓存是如何工作的?:对AFImageCache & NSUrlCache 解释
- (译)缓存在AFNetworking中是如何工作的?AFImageCache和NSUrlCache给你答案
- (译)缓存在AFNetworking中是如何工作的?AFImageCache和NSUrlCache给你答案
- (译)缓存在AFNetworking中是如何工作的?AFImageCache和NSUrlCache给你答案
- AFNetWorking是如何进行数据缓存的--之AFImageCache & NSURLCache 详解
- How does netstat work
- How does XVCL work?
- How does JNA work?
- How does cas work
- how does wifi work?
- How does google work
- How does browsersync work?
- How does maven work?
- How Does SSP Work in Juniper Networks NSM?
- Leetcode dfs N-Queens II
- 一个好用又快速的轻量级mysql管理工具——HeidiSQL
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
- Cocos2d-x函数----addChild
- 替代NSLog
- How Does Caching Work in AFNetworking? : AFImageCache & NSUrlCache Explained
- 共享windows7网络时出现依赖的组件无法启动的解决办法
- iPhone6、iPhone6Plus分辨率
- UDP/TCP与fork
- Android中限制EditText中输入Emoji表情e
- 4.1 PCIe总线的基础知识
- Hybrid
- 第190天
- String s = null ; String s = "" ; String s ;