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

//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 请求失败后,取消网络请求减少内存消耗)