美文网首页
iOS 请求失败后,取消网络请求减少内存消耗

iOS 请求失败后,取消网络请求减少内存消耗

作者: Timer丶 | 来源:发表于2018-06-05 16:06 被阅读21次

//DD_V_T_APIClient 继承自 AFHTTPSessionManager

- (void)cancelRequet:(NSString *)requestPath {

//1.先获取所有的任务,包括请求,下载,上传等

    [[[DD_V_T_APIClient sharedClient] session] getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {

//2.调用取消请求

        [self cancelTasksInArray:dataTasks withPath:requestPath];

        [self cancelTasksInArray:uploadTasks withPath:requestPath];

        [self cancelTasksInArray:downloadTasks withPath:requestPath];

    }];

}

- (void)cancelTasksInArray:(NSArray *)tasksArray withPath:(NSString *)path {

//3.遍历该请求类型下所有的网络请求

    for (NSURLSessionTask *task in tasksArray) {

        NSRange range = [[[[task currentRequest] URL] absoluteString] rangeOfString:path];

//4.如果该请求的地址找不到,取消任务

        if (range.location != NSNotFound) {

            [task cancel];

        }

    }

}

相关文章

网友评论

      本文标题:iOS 请求失败后,取消网络请求减少内存消耗

      本文链接:https://www.haomeiwen.com/subject/xhfmsftx.html