iOS网络请求太频繁 处理之前发送的http请求(取消)

搜索功能在APP中非常的常见,搜索功能伴随的往往是实时搜索结果,极大的方便了用户的查找与实时数据的更新,但是也有极大的问题,当我们搜索框的文字改变的时候,就会进行网络请求,如果输入特别快的时候,网络请求也会特频繁,对服务器的压力也就更大。

解决方法:在进行新的网络请求的时候,把之前的http请求取消掉,保证同一时间只有一个http请求在执行,这样就极大的优化了性能,同时减小了服务器的压力

下面我们用常用的网络请求第三方-----AFNetWorking为例简单明了的演示一下

大致思路:声明一个全局的 AFHTTPSessionManager *manager;再声明一个全局的可变数组用于存放 http请求。

PS:进行http请求的时候,一定要创建一个新的 NSURLSessionDataTask 任务

再PS:[task cancel]之后,并不是说这个http请求就完全没有任何音讯了,task会立刻回掉 error,NSLog(@"error:%@",error.description);可以打印log消息看到Code=-999 “已取消”

 

上代码

#import "LPDemoVC.h"

#import <AFNetworking.h>

@interface LPDemoVC ()

@property (nonatomic,strong)    AFHTTPSessionManager    *manager;
@property (nonatomic,strong)    NSMutableArray          *taskArray;

@end

@implementation LPDemoVC

#pragma mark -- taskArr
-(NSMutableArray *)taskArray{
    if (!_taskArray) {
        _taskArray = [NSMutableArray arrayWithCapacity:3];
    }
    return _taskArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //初始化  AFHTTPSessionManager 并且设置配置信息,在这不做多述
    self.manager = [AFHTTPSessionManager manager];
    self.manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
}

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //获取当前输入字符串,并且调用网络数据
    NSString *searchStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    [self loadDataWithSearchStr:searchStr];
    return YES;
}

-(void)loadDataWithSearchStr:(NSString *)searchStr{
    //首先遍历存放task的数组,并且取消之前的http请求
    [self.taskArray enumerateObjectsUsingBlock:^(NSURLSessionDataTask *task, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"这个任务取消之前的状态是:::::%ld",task.state);
        [task cancel];//取消http请求
        NSLog(@"这个任务取消之后的状态是:::::%ld",task.state);
    }];
    //移除self.taskArray中所有的任务
    [self.taskArray removeAllObjects];
    //创建一个新的NSURLSessionDataTask,记住,一定是新创建一个任务
    NSURLSessionDataTask *newTask = [self.manager GET:@"你的接口url" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"success:%@",JSON);
        //在这里处理数据
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error:%@",error.description);
    }];
    //把最新的任务存进self.taskArray,用于下次遍历
    [self.taskArray addObject:newTask];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

这里给出NSURLSessionTaskState的状态码

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);

搞定!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值