In iOS application development, in order to reduce the number of interactions with the server, to speed up the user's response speed, generally in the iOS device to add a caching mechanism. 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. Here's how to cache in an iOS device.
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.
6, Nsurlrequestreloadrevalidatingcachedata: Verify the local data and remote data is the same, if different download remote data, 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:
#import
@interface Viewcontroller:uiviewcontroller
@property (Strong, nonatomic) nsurlconnection *connection;
@property (Strong, nonatomic) Nsurlcache *urlcache;
@property (Strong, nonatomic) Nsurl *url;
@property (Strong, nonatomic) Nsmutableurlrequest *request;
-(Ibaction) Reloadwebview: (UIButton *) sender;
@end
#import "ViewController.h"
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) viewdidload
{
[Super Viewdidload];
NSString *paramurlasstring= @ "http://blog.sina.com.cn/u/2526279194";
Self.urlcache = [Nsurlcache Sharedurlcache];
[Self.urlcache setmemorycapacity:1*1024*1024];
Create a Nsurl
Self.url = [Nsurl urlwithstring:paramurlasstring];
Create a request
Self.request=[nsmutableurlrequest RequestWithURL:self.url
Cachepolicy:nsurlrequestuseprotocolcachepolicy
TIMEOUTINTERVAL:30.0F];
[Self.mywebview LoadRequest:self.request];
}
In this example, we request a URL for the http://blog.sina.com.cn/u/2526279194 site. If the URL is cached, we get the data directly from the cache, or we retrieve the data from the http://blog.sina.com.cn/u/2526279194 site. We set the cache size to 1M.
-(Ibaction) Reloadwebview: (UIButton *) Sender {
Get the cache output from the request
Nscachedurlresponse *response =[self.urlcache CachedResponseForRequest:self.request];
Determine if there is a cache
if (response! = nil) {
NSLog (@ "If there is cache output, fetch data from cache");
[Self.request Setcachepolicy:nsurlrequestreturncachedatadontload];
}
[Self.mywebview LoadRequest:self.request];
Self.connection = nil;
Nsurlconnection *newconnection = [[Nsurlconnection alloc] InitWithRequest:self.request
Delegate:self
Startimmediately:yes];
Self.connection = newconnection;
}
Using the code below, I'll print out the requested process
-(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");
}
@end
The first print results are as follows
2013-01-31 15:28:29.923 nsurlcachedemo[27848:907] Send request soon
2013-01-31 15:28:30.043 nsurlcachedemo[27848:907] will receive output
2013-01-31 15:28:30.045 nsurlcachedemo[27848:907] Accept Data
2013-01-31 15:28:30.047 nsurlcachedemo[27848:907] data length is = 30047
2013-01-31 15:28:30.095 nsurlcachedemo[27848:907] Accept Data
2013-01-31 15:28:30.098 nsurlcachedemo[27848:907] data length is = 3575
2013-01-31 15:28:30.102 nsurlcachedemo[27848:907] Accept Data
2013-01-31 15:28:30.104 nsurlcachedemo[27848:907] number according length is = 1482
2013-01-31 15:28:30.105 nsurlcachedemo[27848:907] the cache output
2013-01-31 15:28:30.107 nsurlcachedemo[27848:907] Request Complete
The second click Prints the results as follows
2013-01-31 15:28:31.599 nsurlcachedemo[27848:907] If you have cached output, get the data from the cache
2013-01-31 15:28:31.607 nsurlcachedemo[27848:907] Send request soon
2013-01-31 15:28:31.840 nsurlcachedemo[27848:907] will receive output
2013-01-31 15:28:31.843 nsurlcachedemo[27848:907] Accept Data
2013-01-31 15:28:31.845 nsurlcachedemo[27848:907] data length is = 35104
2013-01-31 15:28:31.846 nsurlcachedemo[27848:907] Request Complete
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.
Ios-nsurlcache Memory Cache