ios url缓存策略——NSURLCache、 NSURLRequest
来源:互联网 发布:十几万买域名 编辑:程序博客网 时间:2024/06/04 19:23
一:url 缓存策略
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];
}
- 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一些缓存策略值
- C#针对datagridview 操作
- Object C 预处理程序
- Android:浅谈WebView
- 如何获取android控件的高度
- swift中类和结构体的区别
- ios url缓存策略——NSURLCache、 NSURLRequest
- java输入输出流实例
- Android:WebView全面详解
- iOS7最佳实践:一个天气App案例(二)
- NSSearchPathForDirectoriesInDomains和NSHomeDirectory()
- 黑马程序员_Java面向对象之封装、继承和多态学习笔记
- Eclipse.ini的相关说明
- dell 服务器raid配置
- 变形课