zoukankan      html  css  js  c++  java
  • iOS之NSURLCache、ASI设置缓存及使用步骤

      1 一NSURLCache
      2 缓存的使用步骤
      3 // 获得全局的缓存对象
      4 NSURLCache *cache = [NSURLCache sharedURLCache];
      5 
      6 // 设置缓存容量
      7 cache.memoryCapacity = 1024 * 1024;
      8 cache.diskCapacity = 20 * 1024 * 1024;
      9 
     10 // 设置请求的缓存策略
     11 request.cachePolicy = NSURLRequestReturnCacheDataElseLoad;
     12 
     13 二、ASI
     14 1.缓存的使用步骤
     15 1> 缓存单个请求
     16 // 1.获得全局的缓存对象(决定缓存的存储路径, 存储到什么地方)
     17 ASIDownloadCache *cache = [ASIDownloadCache sharedCache];
     18 // 设置默认的缓存加载策略
     19 cache.defaultCachePolicy = ASIDoNotReadFromCacheCachePolicy;
     20 
     21 // 2.设置请求对象的缓存对象(使用哪个缓存对象)
     22 request.downloadCache = cache;
     23 
     24 // 3.设置请求对象的缓存加载策略
     25 request.cachePolicy = ASIOnlyLoadIfNotCachedCachePolicy;
     26 // 如果没有缓存, 才发送请求
     27 
     28 // 4.设置请求对象的缓存存储策略(存储的时长)
     29 request.cacheStoragePolicy = ASICachePermanentlyCacheStoragePolicy;
     30 // 永久存储
     31 
     32 注意, 缓存加载策略的优先级 : request.cachePolicy > cache.defaultCachePolicy
     33 
     34 2> 缓存所有请求
     35 // 1.获得全局的缓存对象(决定缓存的存储路径, 存储到什么地方)
     36 ASIDownloadCache *cache = [ASIDownloadCache sharedCache];
     37 // 设置默认的缓存加载策略
     38 cache.defaultCachePolicy = ASIOnlyLoadIfNotCachedCachePolicy;
     39 
     40 // 2.设置全局缓存对象
     41 [ASIHTTPRequest setDefaultCache:cache];
     42 
     43 2.发送请求
     44 1> 同步请求
     45 [request startSynchronous];
     46 
     47 2> 异步请求
     48 [request startAsynchronous];
     49 
     50 3.GETPOST
     51 1> GET请求
     52 ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
     53 
     54 2> POST请求
     55 ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
     56 // 添加普通参数(非文件参数)
     57 [request setPostValue:@"zhangsan" forKey:@"username"];
     58 [request setPostValue:@"123" forKey:@"pwd"];
     59 
     60 4.文件下载
     61 // 文件的存储路径(文件下载到什么地方)
     62 request.downloadDestinationPath = filepath;
     63 // 设置下载代理(监听下载进度)
     64 request.downloadProgressDelegate = self.circleView;
     65 // 支持断点下载
     66 request.allowResumeForFileDownloads = YES;
     67 
     68 5.文件上传
     69 // 添加文件参数(file : 需要上传文件的路径)
     70 [request setFile:file forKey:@"file"];
     71 [request setFile:file withFileName:@"123.txt" andContentType:@"text/plain" forKey:@"file"];
     72 [request setData:data withFileName:@"minion.png" andContentType:@"image/png" forKey:@"file"];
     73 
     74 // 设置上传代理(监听上传进度)
     75 request.uploadProgressDelegate = self.circleView;
     76 
     77 6.监听请求的过程
     78 1> 代理方法
     79 // 设置代理
     80 request.delegate = self;
     81 // 遵守协议
     82 ASIHTTPRequestDelegate
     83 // 实现协议中的代理方法
     84 - (void)requestStarted:(ASIHTTPRequest *)request;
     85 - (void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)data
     86 - (void)requestFinished:(ASIHTTPRequest *)request;
     87 - (void)requestFailed:(ASIHTTPRequest *)request;
     88 
     89 2> SEL
     90 // 设置代理
     91 request.delegate = self;
     92 // 设置方法名
     93 [request setDidStartSelector:@selector(start)]; // 开始发送请求, 就会调用代理的start方法
     94 // ....
     95 
     96 3> block
     97 [request setStartedBlock:^{
     98     NSLog(@"setStartedBlock ----");
     99 }];
    100 
    101 [request setDataReceivedBlock:^(NSData *data) {
    102     NSLog(@"setDataReceivedBlock ----");
    103 }];
    104 
    105 [request setCompletionBlock:^{
    106     NSLog(@"setCompletionBlock ----");
    107 }];
    108 
    109 [self setFailedBlock:^{
    110     NSLog(@"setFailedBlock ----");
    111 }];
    112 
    113 7.通过request对象获得服务器的响应
    114 1> 获得响应头信息
    115 @property (atomic, retain) NSDictionary *responseHeaders;
    116 
    117 2> 获得响应体(实体内容)
    118 - (NSData *)responseData; // 直接返回服务器的二进制数据
    119 - (NSString *)responseString; // 将二进制数据转成字符串(方便调试)
  • 相关阅读:
    Linux系统运维之MYSQL数据库集群部署(主从复制)
    Linux系统运维之负载均衡Tengine
    Linux系统运维之subversionEdge部署
    Linux系统运维之Hadoop、Hive、Flume数据处理
    CoIDE在STM32系列单片机中的使用实践
    软硬件调试九法:第三条规则 不要想而要看
    《产品经理》读书笔记
    <读书笔记> 代码整洁之道
    关于鼠标手的症状和恢复方法
    <读书笔记>软件调试之道 :从大局看调试-理想的调试环境
  • 原文地址:https://www.cnblogs.com/changxs/p/4048443.html
Copyright © 2011-2022 走看看