主要精髓在于 
第一点:不要initialize a new AFHTTPSessionManager object everytime 一定要把manager用成全局的 
第二点:把请求返回的task对象丢进数组,下次触发的时候把遍历数组,把之前的所有任务[task cancel]

- (void)viewDidLoad {
[super viewDidLoad];
/// create the AFHTTPSessionManager object, we're gonna use it in every request
self.manager = [[AFHTTPSessionManager alloc] init];
self.manager.responseSerializer = [AFJSONResponseSerializer serializer];
/// create an array that is going to hold the requests task we've sent to the server. so we can get back to them later
self.arrayOfTasks = [NSMutableArray new];
/// discussion:
/// an array holds multiple objects. if you just want to hold a ref to the latest task object
/// then create a property of NSURLSessionDataTask instead of NSMutableArray, and let it point to the latest NSURLSessionDataTask object you create
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;{
/// your code goes here
/// .....
/// .....
/// .....
/// .....
/// till we reach
if(stringLength>=3){
/// cancel all previous tasks
[self.arrayOfTasks enumerateObjectsUsingBlock:^(NSURLSessionDataTask *taskObj, NSUInteger idx, BOOLBOOL *stop) {
[taskObj cancel]; /// when sending cancel to the task failure: block is going to be called
}];
/// empty the arraOfTasks
[self.arrayOfTasks removeAllObjects];
/// init new task
NSURLSessionDataTask *task = [self.manager GET:urlString parameters:nil success:^(NSURLSessionDataTask *task, id responseObject){
/// your code
}failure:^(NSURLSessionDataTask *task, NSError *error){
/// your code
}];
/// add the task to our arrayOfTasks
[self.arrayOfTasks addObject:task];
}
return YES;
}

注意: 
这里的task cancel亲测确实能把网络请求cancel掉,可以看下面的log,记住一点,这里cancel之后不代表就不回调 
了,只是会回调到error的那个block里面,各位需要信息的可以测试下,在error打个断点就能调试出来了

// 查看网络任务的状态
[articleInterface.articleArrayTask enumerateObjectsUsingBlock:^(NSURLSessionDataTask *taskObj, NSUInteger idx, BOOLBOOL *stop) {
DDLogVerbose(@"当前的文章删除前网络任务状态是==================>>>>>%ld",taskObj.state);
[taskObj cancel]; /// when sending cancel to the task failure: block is going to be called
DDLogVerbose(@"当前的文章删除后网络任务状态是==================>>>>>%ld",taskObj.state);
}];
typedef NS_ENUM(NSInteger, NSURLSessionTaskState) {
NSURLSessionTaskStateRunning = 0, /* The task is currently being serviced by the session */
NSURLSessionTaskStateSuspended = 1,
NSURLSessionTaskStateCanceling = 2, /* The task has been told to cancel. The session will receive a URLSession:task:didCompleteWithError: message. */
NSURLSessionTaskStateCompleted = 3, /* The task has completed and the session will receive no more delegate notifications */
} NS_ENUM_AVAILABLE(NSURLSESSION_AVAILABLE, 7_0); 转自:http://blog.5ibc.net/p/89130.html

iOS开发中如遇到频繁的Http请求,取消之前已经发送的Http的更多相关文章

  1. iOS开发中的Get请求和POST请求

    //Get请求一般为不涉及到用户的账号密码的网络请求,其中Get请求是等请求内容回来之后,才可以进行下一步的操作 - (void)requestWithGet{ //Get请求: //1.设置请求路径 ...

  2. iOS开发中关于UIImage的知识点总结

    UIImage是iOS中层级比较高的一个用来加载和绘制图像的一个类,更底层的类还有 CGImage,以及iOS5.0以后新增加的CIImage.今天我们主要聊一聊UIImage的三个属性: image ...

  3. iOS开发中常见的一些异常

    iOS开发中常见的异常包括以下几种NSInvalidArgumentExceptionNSRangeExceptionNSGenericExceptionNSInternallnconsistency ...

  4. 总结iOS开发中的断点续传那些事儿

    前言 断点续传概述 断点续传就是从文件赏赐中断的地方重新开始下载或者上传数据,而不是从头文件开始.当下载大文件的时候,如果没有实现断点续传功能,那么每次出现异常或者用户主动的暂停,都会从头下载,这样很 ...

  5. iOS开发中静态库之".framework静态库"的制作及使用篇

    iOS开发中静态库之".framework静态库"的制作及使用篇 .framework静态库支持OC和swift .a静态库如何制作可参照上一篇: iOS开发中静态库之" ...

  6. iOS开发中静态库制作 之.a静态库制作及使用篇

    iOS开发中静态库之".a静态库"的制作及使用篇 一.库的简介 1.什么是库? 库是程序代码的集合,是共享程序代码的一种方式 2.库的类型? 根据源代码的公开情况,库可以分为2种类 ...

  7. ios开发中的小技巧

    在这里总结一些iOS开发中的小技巧,能大大方便我们的开发,持续更新. UITableView的Group样式下顶部空白处理 //分组列表头部空白处理 UIView *view = [[UIViewal ...

  8. IOS 开发中 Whose view is not in the window hierarchy 错误的解决办法

    在 IOS 开发当中经常碰到 whose view is not in the window hierarchy 的错误,该错误简单的说,是由于 "ViewController" ...

  9. [转]iOS开发中的火星坐标系及各种坐标系转换算法

     iOS开发中的火星坐标系及各种坐标系转换算法 源:https://my.oschina.net/u/2607703/blog/619183   其原理是这样的:保密局开发了一个系统,能将实际的坐标转 ...

随机推荐

  1. python中常用第三方库记录

    python中有很多很好用的第三方库,现在记录一下这些库以及如何下载 一.virtualenv,这是一个可以将生产环境隔离开的python库,非常好用 在linux下使用pip install vir ...

  2. GPU bubbles

    https://software.intel.com/en-us/articles/performance-analysis-and-optimization-for-pc-based-vr-appl ...

  3. 解决安装mysqlclient出现问题:mysql_config: not found

    解决安装mysqlclient出现如下问题: Complete output from command python setup.py egg_info: /bin/sh: : mysql_confi ...

  4. Mysql数据库配置文件my.cnf详解

    basedir = path 使用给定目录作为根目录(安装目录). character-sets-dir = path 给出存放着字符集的目录. datadir = path 从给定目录读取数据库文件 ...

  5. 组件prop检验

    Vue.js中的父子组件相信都已经是大家很常用到的功能了, 父组件通过props属性向子组件传值子组件通过自定义事件向父组件传值 那么我们怎么去校验props属性中的类型呢 笔者列出以下几种方法: 1 ...

  6. About stats collected

    pg_class.relpages pg_class.reltuples仅仅是近似值,和实际数据会有点误差: 新建空表.首次insert对自己主动收集和更新统计信息,影响的表pg_class\pg_s ...

  7. Redis 数据结构之简单动态字符串SDS

    几个概念1:key对象 数据库存储键值对的键,总是一个字符串对象.2:value对象 数据库存储键值对的值,可以是字符串对象,list对象,hash对象,set对象,sorted set对象.    ...

  8. 解决虚拟机 centos 网络服务启动

    现象: 1. 通过 ip addr 显示 eno16777736 适配器为 DOWN 状态 2. service status network 显示以下日志: .... 11月 05 15:30:10 ...

  9. JavaScript经常使用代码段

    把例如以下代码增加<body>区域中: 后退 前进 <!--<input type="button" value="后退" onClic ...

  10. 单元测试JUnit 4(二)——keeps the bar green to keeps the code clean

    1.Failure和Error Failure是指测试失败  Error是指测试程序本身出错  (int a=10/0) 2.JUnit常用注解 2.1 @RunWith: 可以更改测试运行器(继承o ...