ios url缓存策略——NSURLCache、 NSURLRequest
来源:互联网 发布:看小电影的网站知乎 编辑:程序博客网 时间:2024/06/05 14:29
NSURLRequest
requestWithURL:cachePolicy:timeoutInterval:
1:NSURLRequestUseProtocolCachePolicy
This flag will use the underlying protocol’s caching mechanism if the protocol
supports it.
基础策略
2:NSURLRequestReloadIgnoringLocalCacheData
This flag specifies that the local cached copy of the resource that is about to be
downloaded must be disregarded and the remote cache policy must be effective.
If there is a local copy of the resource, managed by the framework itself, it will be
ignored.
忽略本地缓存
3:NSURLRequestReturnCacheDataElseLoad
This flag specifies that the cached data must be used before attempting to load the
data from the original source. The cached data could be protocol-based cached or
locally cached. If there is no cached data, the data will be downloaded from the
original source.
首先使用缓存,如果没有本地缓存,才从原地址下载
4:NSURLRequestReturnCacheDataDontLoad
This flag specifies that only the local cached data must be used. If the data has not
been cached, the request will fail. This is a great flag to use whenever your application
wants to perform operations in offline mode (such as the Offline Mode in
web browsers).
使用本地缓存,从不下载,如果本地没有缓存,则请求失败。此策略多用于离线操作
5:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
This flag disregards any type of caching involved in the process, local and remote,
and always attempts to download the data from the original source.
无视任何的缓存策略,无论是本地的还是远程的,总是从原地址重新下载
6:NSURLRequestReloadRevalidatingCacheData
This flag specifies that the original source of data must validate the local cache (if
any) before an attempt is made to download the data from the original source. If
there is a copy of the original source cached locally and the remote source specifies
that the cached data is valid, the data won’t be downloaded again. In any other
case, the data will be downloaded from the original source.
如果本地缓存是有效的则不下载。其他任何情况都从原地址重新下载
二:NSURLCache
NSURLCache *urlCache = [NSURLCache sharedURLCache];
In iOS, NSURLCache supports caching data only in memory and not
on disk.
(NSURLCache 仅支持内存缓存,不支持硬盘缓存)
- - (void) downloadURL:(NSString *)paramURLAsString{
- if ([paramURLAsString length] == 0){
- NSLog(@"Nil or empty URL is given");
- return;
- }
- /* Get the shared URL Cache object. No need to create a new one */
- NSURLCache *urlCache = [NSURLCache sharedURLCache];
- /* We will store up to 1 Megabyte of data into the cache */
- [urlCache setMemoryCapacity:1*1024*1024];
- /* For our request, we need an instance of NSURL so let's retrieve
- that from the string that we created before */
- NSURL *url = [NSURL URLWithString:paramURLAsString];
- /* And this is our request */
- NSMutableURLRequest *request =[NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];
- /* Try to get a cached response to our request.
- This might come back as nil */
- NSCachedURLResponse *response =[urlCache cachedResponseForRequest:request];
- /* Do we have a cached response? */
- if (response != nil){
- NSLog(@"Cached response exists. Loading data from cache...");
- [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];
- }
- self.connection = nil;
- /* Start the connection with the request */
- NSURLConnection *newConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
- self.connection = newConnection;
- [newConnection release];
- }
- // Create the request.
- //所构建的NSURLRequest具有一个依赖于缓存响应的特定策略,cachePolicy取得策略,timeoutInterval取得超时值
- NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
- cachePolicy:NSURLRequestUseProtocolCachePolicy
- timeoutInterval:60.0];
- // create the connection with the request
- // and start loading the data
- NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
- if (theConnection) {
- // Create the NSMutableData to hold the received data.
- // receivedData is an instance variable declared elsewhere.
- receivedData = [[NSMutableData data] retain];
- } else {
- // Inform the user that the connection failed.
- }
- ios url缓存策略——NSURLCache、 NSURLRequest
- ios url缓存策略——NSURLCache、 NSURLRequest、Http规则
- ios url缓存策略——NSURLCache、 NSURLRequest
- ios url缓存策略——NSURLCache、 NSURLRequest
- ios url缓存策略——NSURLCache、 NSURLRequest
- ios url缓存策略——NSURLCache、 NSURLRequest
- ios url缓存策略——NSURLCache、 NSURLRequest
- ios url缓存策略——NSURLCache、 NSURLRequest
- iOS URL缓存策略------------NSURLCache、 NSURLRequest
- iOS网络——NSURLCache设置网络请求缓存,NSUrlCache
- 使用NSURLCache为NSURLRequest设置缓存
- iOS网络——NSURLCache设置网络请求缓存
- iOS开发网络篇—数据缓存NSURLCache
- ios- NSURLRequest缓存策略以及NSURLConnection发送异步请求
- iOS 缓存 NSURLCache类
- iOS -NSURLCache内存缓存
- iOS NSURLCache(网络缓存)
- NSURLCache一些缓存策略值
- 黑马韩前成Linux入门到精通の速搭建EXTmail邮件服务器
- Flex4关于webservice接口传入对象的解决方案
- select函数使用总结
- C#中生成随机数的几种实现思路
- php正则表达式完全教程(三)
- ios url缓存策略——NSURLCache、 NSURLRequest
- 自定义的CustomMultiPartEntity 继承MultipartEntity
- Java String,int,Integer,char 类型转换 ,sizeof,strlen,length的区别 .
- cvs tag and branch
- SAP GUI FOR HTML登陆(作者:梦心)出处:http://www.cnblogs.com/mengxin523/)
- php正则表达式完全教程(四)
- OpenCV-2.4.3+VS2010+Win7(32位)安装与配置
- 和机器学习和计算机视觉相关的数学 (转载)
- MFC拥有WIN7/XP样式的风格