• NSURLRequest 使用(网络文摘)


    使用缓存的目的是为了使用的应用程序能更快速的响应用户输入,是程序高效的运行。有时候我们需要将远程web服务器获取的数据缓存起来,减少对同一个url多次请求。

    内存缓存我们可以使用sdk中的NSURLCache类。NSURLRequest需要一个缓存参数来说明它请求的url何如缓存数据的,我们先看下它的CachePolicy类型。

    1、NSURLRequestUseProtocolCachePolicy NSURLRequest默认的cache policy,使用Protocol协议定义。
    2、NSURLRequestReloadIgnoringCacheData 忽略缓存直接从原始地址下载。
    3、NSURLRequestReturnCacheDataElseLoad 只有在cache中不存在data时才从原始地址下载。
    4、NSURLRequestReturnCacheDataDontLoad 只使用cache数据,如果不存在cache,请求失败;用于没有建立网络连接离线模式;
    5、NSURLRequestReloadIgnoringLocalAndRemoteCacheData:忽略本地和远程的缓存数据,直接从原始地址下载,与NSURLRequestReloadIgnoringCacheData类似。
    6、NSURLRequestReloadRevalidatingCacheData:验证本地数据与远程数据是否相同,如果不同则下载远程数据,否则使用本地数据。

    NSURLCache还提供了很多方法,来方便我们实现应用程序的缓存机制。下面我通过一个例子来说明,这个例子减少我们对同一个url多次请求。看下面代码:

    -(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];

    /* 设置缓存的大小为1M*/

    [urlCache setMemoryCapacity:1*1024*1024];

    //创建一个nsurl

    NSURL *url = [NSURL URLWithString:paramURLAsString];

    //创建一个请求

    NSMutableURLRequest *request =

    [NSMutableURLRequest

    requestWithURL:url

    cachePolicy:NSURLRequestUseProtocolCachePolicy

    timeoutInterval:60.0f];

    //从请求中获取缓存输出

    NSCachedURLResponse *response =

    [urlCache cachedResponseForRequest:request];

    //判断是否有缓存

    if (response != nil){

    NSLog(@”如果有缓存输出,从缓存中获取数据”);

    [request setCachePolicy:NSURLRequestReturnCacheDataDontLoad];

    }

    self.connection = nil;

    /* 创建NSURLConnection*/

    NSURLConnection *newConnection =

    [[NSURLConnection alloc] initWithRequest:request

    delegate:self

    startImmediately:YES];

    self.connection = newConnection;

    [newConnection release];

    }

    这个例子中,我们请求url为www.baidu.com的网站。如果这个url被缓存了,我们直接从缓存中获取数据,否则从www.baidu.com站点上重新获取数据。我们设置了缓存大小为1M。

    使用下面代码,我将请求的过程打印出来:

    - (void)  connection:(NSURLConnection *)connection

    didReceiveResponse:(NSURLResponse *)response{

    NSLog(@”将接收输出”);

    }

    - (NSURLRequest *)connection:(NSURLConnection *)connection

    willSendRequest:(NSURLRequest *)request

    redirectResponse:(NSURLResponse *)redirectResponse{

    NSLog(@”即将发送请求”);

    return(request);

    }

    - (void)connection:(NSURLConnection *)connection

    didReceiveData:(NSData *)data{

    NSLog(@”接受数据”);

    NSLog(@”数据长度为 = %lu”, (unsigned long)[data length]);

    }

    - (NSCachedURLResponse *)connection:(NSURLConnection *)connection

    willCacheResponse:(NSCachedURLResponse *)cachedResponse{

    NSLog(@”将缓存输出”);

    return(cachedResponse);

    }

    - (void)connectionDidFinishLoading:(NSURLConnection *)connection{

    NSLog(@”请求完成”);

    }

    - (void)connection:(NSURLConnection *)connection

    didFailWithError:(NSError *)error{

    NSLog(@”请求失败”);

    }

    当我们第一次点击界面上的按钮,打印的结果如下:

    2011-07-30 18:50:24.910 Caching[3971:207] 即将发送请求

    2011-07-30 18:50:28.557 Caching[3971:207] 将接收输出

    2011-07-30 18:50:31.677 Caching[3971:207] 接受数据

    2011-07-30 18:50:31.681 Caching[3971:207] 数据长度为 = 4414

    2011-07-30 18:50:31.682 Caching[3971:207] 接受数据

    2011-07-30 18:50:31.682 Caching[3971:207] 数据长度为 = 2996

    2011-07-30 18:50:38.107 Caching[3971:207] 将缓存输出

    2011-07-30 18:50:38.109 Caching[3971:207] 请求完成

    在看我们第二次点击界面上的按钮,打印结果如下:

     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] 请求完成

     

    我们看到没有“将缓存输出”一项,请求到的数据是第一次请求的累积,也就是第二次是从内存中获取数据的。

  • 相关阅读:
    hdu_5961_传递(bitset)
    hdu_5963_朋友(找规律)
    hdu_5968_异或密码(预处理+二分)
    hdu_5969_最大的位或(贪心)
    hdu_5965_扫雷(递推)
    hdu_5950_Recursive sequence(矩阵快速幂)
    hdu_5286_wyh2000 and sequence(分块)
    [BZOJ1951][SDOI2005]古代猪文(数论好题)
    [BZOJ2659][WC2012]算不出的算式(几何)
    [BZOJ 2656][ZJOI2012]数列(递归+高精度)
  • 原文地址:https://www.cnblogs.com/tony_cap/p/2505848.html
Copyright © 2020-2023  润新知