链式网络请求其实主要用于管理有相互依赖的网络请求,它实际上最终能够用来管理多个拓扑排序后的网络请求。git
例如,咱们有一个需求,须要用户先发送注册Api,而后获取用户信息Api,最后再获取用户id等信息。github
/// 链式网络请求
/// @param request 请求体系
/// @param success 所有成功回调,存放请求全部结果数据
/// @param failure 失败回调,只要一个失败就会响应
/// @param chain 链式回调,返回下个网络请求体,为空时便可结束后续请求,responseObject上个网络请求响应数据
+ (void)HTTPChainRequest:(__kindof KJNetworkingRequest *)request
success:(KJNetworkChainSuccess)success
failure:(KJNetworkChainFailure)failure
chain:(KJNetworkNextChainRequest)chain,...;
复制代码
/// 链式网络请求,需 `chain` 和 `lastchain` 配合使用
/// @param request 请求体系
/// @param failure 失败回调,只要一个失败就会响应
/// @return 返回自身对象
+ (instancetype)HTTPChainRequest:(__kindof KJNetworkingRequest *)request failure:(KJNetworkChainFailure)failure;
/// 请求体传递载体,回调返回上一个网络请求结果
@property (nonatomic, copy, readonly) KJNetworkChainManager * (^chain)(KJNetworkNextChainRequest);
/// 最后链数据回调,回调最后一个网络请求结果
@property (nonatomic, copy, readonly) void(^lastChain)(void(^)(id responseObject));
复制代码
使用说明:markdown
实例化第一个网络请求体request
,获取到数据回调在chain,而后解析return给第二个网络请求体request
,而后依次类推,最终以**nil
**结尾便可。网络
success回调:可拿到所有网络响应数据,目录和请求顺序一致
failure回调:只要当中某一个网络失败,即响应失败闭包
测试用例:app
// 测试不定参数方式链式网络请求
- (void)testMoreChainNetworking{
XCTestExpectation * expectation = [self expectationWithDescription:@"test more chain."];
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.ip = @"https://www.douban.com";
request.path = @"/j/app/radio/channels";
request.responseSerializer = KJSerializerJSON;
[KJNetworkChainManager HTTPChainRequest:request success:^(NSArray<id> * _Nonnull responseArray) {
NSLog(@"----%@",responseArray);
[expectation fulfill];
} failure:^(NSError * _Nonnull error) {
XCTFail(@"%@", error.localizedDescription);
} chain:^__kindof KJNetworkingRequest * _Nullable(id _Nonnull responseObject) {
NSArray * array = responseObject[@"channels"];
NSDictionary * dict = array[arc4random() % array.count];
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.ip = @"https://www.douban.com";
request.path = [@"/j/app/radio/channels/channel_id=" stringByAppendingFormat:@"%@",dict[@"channel_id"]];
request.responseSerializer = KJSerializerJSON;
return request;
}, ^__kindof KJNetworkingRequest * _Nullable(id _Nonnull responseObject) {
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.ip = @"https://www.douban.com";
request.path = @"/j/app/radio/channels";
request.responseSerializer = KJSerializerJSON;
return request;
}, ^__kindof KJNetworkingRequest * _Nullable(id _Nonnull responseObject) {
NSArray * array = responseObject[@"channels"];
NSDictionary * dict = array[arc4random() % array.count];
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.ip = @"https://www.douban.com";
request.path = [@"/j/app/radio/channels/channel_id=" stringByAppendingFormat:@"%@",dict[@"channel_id"]];
request.responseSerializer = KJSerializerJSON;
return request;
}, nil];
[self waitForExpectationsWithTimeout:300 handler:nil];
}
复制代码
使用说明:dom
实例化第一个网络请求体request
,而后经过链式闭包方式chain获取到上一个响应结果,而后return第二个网络请求体request
,依次类推,最后一个网络请求响应结果则经过lastChain获取responseObject
便可。oop
failure回调:只要当中某一个网络失败,即响应失败post
测试用例:测试
// 测试链式网络请求
- (void)testChainNetworking{
XCTestExpectation * expectation = [self expectationWithDescription:@"test chain."];
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.ip = @"https://www.douban.com";
request.path = @"/j/app/radio/channels";
request.responseSerializer = KJSerializerJSON;
[KJNetworkChainManager HTTPChainRequest:request failure:^(NSError * _Nonnull error) {
XCTFail(@"%@", error.localizedDescription);
}].chain(^__kindof KJNetworkingRequest * _Nullable(id _Nonnull responseObject) {
NSArray * array = responseObject[@"channels"];
NSDictionary * dict = array[arc4random() % array.count];
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.ip = @"https://www.douban.com";
request.path = [@"/j/app/radio/channels/channel_id=" stringByAppendingFormat:@"%@",dict[@"channel_id"]];
request.responseSerializer = KJSerializerJSON;
return request;
}).lastChain(^(id _Nonnull responseObject) {
NSLog(@"----%@",responseObject);
[expectation fulfill];
});
[self waitForExpectationsWithTimeout:300 handler:nil];
}
复制代码
大体介绍就差很少这么多吧,其实这类网络使用通常都是在我的中心
pod 'KJNetworkPlugin/Chain'
复制代码