The previous article describes how to upload and download files, and this article describes how to cache on iOS devices.
This article will only cover caching content in memory, and the next article describes caching content on iOS disks.
The purpose of caching is to use applications that can respond more quickly to user input, which is a program that runs efficiently. Sometimes we need to cache the data obtained by the remote Web server and reduce multiple requests to the same URL.
Memory cache We can use the Nsurlcache class in the SDK. Nsurlrequest needs a cache parameter to describe the URL it requested to cache the data, let's look at its cachepolicy type first.
1. Nsurlrequestuseprotocolcachepolicy nsurlrequest The default cache policy, using the Protocol protocol definition.
2, Nsurlrequestreloadignoringcachedata ignore the cache directly from the original address download.
3. Nsurlrequestreturncachedataelseload is downloaded from the original address only if no data exists in the cache.
4, Nsurlrequestreturncachedatadontload only use the cache data, if the cache does not exist, the request failed, for not establishing a network connection offline mode;
5, Nsurlrequestreloadignoringlocalandremotecachedata: Ignore the local and remote cache data, directly from the original address download, Similar to Nsurlrequestreloadignoringcachedata.
6NSURLRequestReloadRevalidatingCacheData: Verify that local data is the same as remote data, and download remote data if different, otherwise use local data.
Nsurlcache also provides a number of ways to facilitate our application caching mechanisms. Let me illustrate with an example that this example reduces our requests to the same URL multiple times. Look at the following code:
[Plain]View Plaincopy
- -(Ibaction) ButtonPress: (ID) sender
- {
- NSString *paramurlasstring= @ "http://www.baidu.com/";
- if ([paramurlasstring length] = = 0) {
- NSLog (@ "Nil or empty URL is given");
- Return
- }
- Nsurlcache *urlcache = [Nsurlcache Sharedurlcache];
- /* Set the cache size to 1m*/
- [Urlcache setmemorycapacity:1*1024*1024];
- Create a Nsurl
- Nsurl *url = [Nsurl urlwithstring:paramurlasstring];
- Create a request
- Nsmutableurlrequest *request =
- [Nsmutableurlrequest
- Requestwithurl:url
- Cachepolicy:nsurlrequestuseprotocolcachepolicy
- TIMEOUTINTERVAL:60.0F];
- Get the cache output from the request
- Nscachedurlresponse *response =
- [Urlcache Cachedresponseforrequest:request];
- Determine if there is a cache
- if (response! = nil) {
- NSLog (@ "If there is cache output, fetch data from cache");
- [Request Setcachepolicy:nsurlrequestreturncachedatadontload];
- }
- Self.connection = nil;
- /* Create nsurlconnection*/
- Nsurlconnection *newconnection =
- [[Nsurlconnection alloc] Initwithrequest:request
- Delegate:self
- Startimmediately:yes];
- Self.connection = newconnection;
- [Newconnection release];
- }
In this example, we request a URL for the www.baidu.com site. If the URL is cached, we get the data directly from the cache, or we retrieve the data from the www.baidu.com site. We set the cache size to 1M.
Using the code below, I will print out the requested process:
[Plain]View Plaincopy
- -(void) connection: (Nsurlconnection *) connection
- Didreceiveresponse: (Nsurlresponse *) response{
- NSLog (@ "will receive output");
- }
- -(Nsurlrequest *) connection: (Nsurlconnection *) connection
- Willsendrequest: (nsurlrequest *) request
- Redirectresponse: (Nsurlresponse *) redirectresponse{
- NSLog (@ "outgoing request");
- return (request);
- }
- -(void) connection: (Nsurlconnection *) connection
- Didreceivedata: (NSData *) data{
- NSLog (@ "Accept data");
- NSLog (@ "Data length =%lu", (unsigned long) [date]);
- }
- -(Nscachedurlresponse *) connection: (Nsurlconnection *) connection
- Willcacheresponse: (Nscachedurlresponse *) cachedresponse{
- NSLog (@ "will cache output");
- return (Cachedresponse);
- }
- -(void) connectiondidfinishloading: (nsurlconnection *) connection{
- NSLog (@ "request Complete");
- }
- -(void) connection: (Nsurlconnection *) connection
- Didfailwitherror: (Nserror *) error{
- NSLog (@ "request failed");
- }
When we first clicked on the button on the interface, the result was printed as follows:
[HTML]View Plaincopy
- 2011-07-30 18:50:24.910 caching[3971:207] Send request soon
- 2011-07-30 18:50:28.557 caching[3971:207] will receive output
- 2011-07-30 18:50:31.677 caching[3971:207] Accept data
- 2011-07-30 18:50:31.681 caching[3971:207] Data length = 4414
- 2011-07-30 18:50:31.682 caching[3971:207] Accept data
- 2011-07-30 18:50:31.682 caching[3971:207] Data length = 2996
- 2011-07-30 18:50:38.107 caching[3971:207] will cache output
- 2011-07-30 18:50:38.109 caching[3971:207] Request complete
After looking at our second click on the button on the interface, the print results are as follows:
2011-07-30 18:52:18.894 Caching[3971:207] 即将发送请求 |
2011-07-30 18:52:18.895 Caching[3971:207] 将接收输出 |
2011-07-30 18:52:18.895 Caching[3971:207] 接受数据 |
2011-07-30 18:52:18.896 Caching[3971:207] 数据长度为 = 7410 |
2011-07-30 18:52:18.896 Caching[3971:207] 请求完成 |
We see that there is no "cache output", and the requested data is the accumulation of the first request, that is, the second time the data is fetched from memory.
Summary: This article briefly describes the iOS memory caching mechanism, the next article will focus on the local caching mechanism