AFNetworking 3.1.0 使用中某些知识点讲解
来源:互联网 发布:海信智能网络电视 编辑:程序博客网 时间:2024/06/05 19:26
# 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];
#########
- /**
- * 取消所有的网络请求
- * a finished (or canceled) operation is still given a chance to execute its completion block before it iremoved from the queue.
- */
- +(void)cancelAllRequest
- {
- [[BJAppClient sharedClient].operationQueue cancelAllOperations];
- }
- #pragma mark - 取消指定的url请求/
- /**
- * 取消指定的url请求
- *
- * @param requestType 该请求的请求类型
- * @param string 该请求的完整url
- */
- +(void)cancelHttpRequestWithRequestType:(NSString *)requestType
- requestUrlString:(NSString *)string
- {
- NSError * error;
- /**根据请求的类型 以及 请求的url创建一个NSMutableURLRequest---通过该url去匹配请求队列中是否有该url,如果有的话 那么就取消该请求*/
- NSString * urlToPeCanced = [[[[BJAppClient sharedClient].requestSerializer
- requestWithMethod:requestType URLString:string parameters:nil error:&error] URL] path];
- for (NSOperation * operation in [BJAppClient sharedClient].operationQueue.operations) {
- //如果是请求队列
- if ([operation isKindOfClass:[NSURLSessionTask class]]) {
- //请求的类型匹配
- BOOL hasMatchRequestType = [requestType isEqualToString:[[(NSURLSessionTask *)operation currentRequest] HTTPMethod]];
- //请求的url匹配
- BOOL hasMatchRequestUrlString = [urlToPeCanced isEqualToString:[[[(NSURLSessionTask *)operation currentRequest] URL] path]];
- //两项都匹配的话 取消该请求
- if (hasMatchRequestType&&hasMatchRequestUrlString) {
- [operation cancel];
- }
- }
- }
- }
相关链接:
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
阅读全文
0 0
- AFNetworking 3.1.0 使用中某些知识点讲解
- AFNetWorking 中使用HTTPS
- iOS讲解迷惑--AFNetworking的使用
- ios中AFNetworking的使用
- AFNetworking中operation的使用
- AFNetworking 使用中错误总结
- iOS 使用AFNetworking 3.1.0如何在请求头中加入cookie
- AFNetWorking知识点总结
- AFNetworking知识点之AFURLSessionManager
- AFNetworking、ASIHTTPRequest中SSL的使用
- Xcode中使用AFNetworking下载文件
- AFNetworking、ASIHTTPRequest中SSL的使用
- 如何在Swift 中使用AFNetworking
- iOS开发中AFNetworking的基本使用
- AFNetworking、ASIHTTPRequest中SSL的使用
- iOS 中 AFNetworking HTTPS 的使用
- AFNetworking 3.0+中使用body传数据
- iOS 中 AFNetworking HTTPS 的使用
- 相关性检验之Pearson系数及python实现
- Hadoop demo 找出共同好友
- 利用OkHttp实现数据持久化存储的方案
- 监听ViewFlipper滑动的子View
- Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(
- AFNetworking 3.1.0 使用中某些知识点讲解
- BZOJ 4423: [AMPPZ2013]Bytehattan 并查集 平面图转对偶图
- 最大流最小割与“Graph Cuts”
- 使用ajax轮询做实时数据更新
- leetcode 513. Find Bottom Left Tree Value
- Oracle之SQL语句性能优化(34条优化方法)
- iOS正则表达式
- Nginx反向代理缓存 + CND+PHP缓存
- git 设置不需要输入密码, 去除 fetch / pull 代码每次都需要输入密码的烦恼