星空网 > 软件开发 > 操作系统

AFNetworking 3.1.0 使用中某些知识点讲解

 

# POST / GET  请求

  /*!

     首先要知道,POST请求不能被缓存,只有 GET 请求能被缓存。因为从数学的角度来讲,GET 的结果是 幂等 的,就好像字典里的 key 与 value 就是幂等的,而 POST 不 幂等 。缓存的思路就是将查询的参数组成的值作为 key ,对应结果作为value。从这个意义上说,一个文件的资源链接,也叫 GET 请求,下文也会这样看待。

     80%的缓存需求:两行代码就可满足

     设置缓存只需要三个步骤:

     

     第一个步骤:请使用 GET 请求。

     

     第二个步骤:

     

     如果你已经使用 了 GET 请求,iOS 系统 SDK 已经帮你做好了缓存。你需要的仅仅是设置下内存缓存大小、磁盘缓存大小、以及缓存路径。甚至这两行代码不设置也是可以的,会有一个默认值。代码如下:

     要注意

     iOS 5.0开始,支持磁盘缓存,但仅支持 HTTP

     iOS 6.0开始,支持 HTTPS 缓存

      */

    NSURLCache *urlCache = [[NSURLCache alloc] initWithMemoryCapacity:4 * 1024 * 1024 diskCapacity:20 * 1024 * 1024 diskPath:nil];

    [NSURLCache setSharedURLCache:urlCache];

#########

  1. /** 
  2.  *  取消所有的网络请求 
  3.  *  a finished (or canceled) operation is still given a chance to execute its completion block before it iremoved from the queue. 
  4.  */  
  5.   
  6. +(void)cancelAllRequest  
  7. {  
  8.     [[BJAppClient sharedClient].operationQueue cancelAllOperations];  
  9. }  
  10.   
  11.   
  12.   
  13. #pragma mark -   取消指定的url请求/  
  14. /** 
  15.  *  取消指定的url请求 
  16.  * 
  17.  *  @param requestType 该请求的请求类型 
  18.  *  @param string      该请求的完整url 
  19.  */  
  20.   
  21. +(void)cancelHttpRequestWithRequestType:(NSString *)requestType  
  22.                        requestUrlString:(NSString *)string  
  23. {  
  24.     NSError * error;  
  25.     /**根据请求的类型 以及 请求的url创建一个NSMutableURLRequest---通过该url去匹配请求队列中是否有该url,如果有的话 那么就取消该请求*/  
  26.     NSString * urlToPeCanced = [[[[BJAppClient sharedClient].requestSerializer  
  27.                                   requestWithMethod:requestType URLString:string parameters:nil error:&error] URL] path];  
  28.       
  29.     for (NSOperation * operation in [BJAppClient sharedClient].operationQueue.operations) {  
  30.         //如果是请求队列  
  31.         if ([operation isKindOfClass:[NSURLSessionTask class]]) {  
  32.             //请求的类型匹配  
  33.             BOOL hasMatchRequestType = [requestType isEqualToString:[[(NSURLSessionTask *)operation currentRequest] HTTPMethod]];  
  34.             //请求的url匹配  
  35.             BOOL hasMatchRequestUrlString = [urlToPeCanced isEqualToString:[[[(NSURLSessionTask *)operation currentRequest] URL] path]];  
  36.             //两项都匹配的话  取消该请求  
  37.             if (hasMatchRequestType&&hasMatchRequestUrlString) {  
  38.                 [operation cancel];  
  39.             }  
  40.         }  
  41.     }  
  42. }  

  

相关链接:

https://github.com/boai/BANetManager
http://www.jianshu.com/p/6856bd9050fc
http://blog.csdn.net/heberan/article/details/51567165
NSURLCache
http://www.cnblogs.com/cbw1987/p/5910624.html



原标题:AFNetworking 3.1.0 使用中某些知识点讲解

关键词:net

net
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流