關於批量網絡請求,平時使用的場景是很是之多,因而乎就封裝該批量插件版網絡請求,下面介紹一下使用技巧。git
這裏主要分爲兩個大類:github
/// 設置網絡併發最大數量,默認5條
@property (nonatomic, assign) NSInteger maxQueue;
/// 設置最大失敗重調次數,默認3次
@property (nonatomic, assign) NSInteger againCount;
/// 網絡鏈接失敗重連時機,默認 KJBatchReRequestOpportunityNone
@property (nonatomic, assign) KJBatchReRequestOpportunity opportunity;
/// 批量請求體,
@property (nonatomic, strong) NSArray<__kindof KJNetworkingRequest *> * requestArray;
複製代碼
批量插件網絡請求,這裏提供設置最大併發數量,失敗調用次數,錯誤重連時機等配置信息數組
/// 批量網絡請求
/// @param configuration 批量請求配置信息
/// @param reconnect 網絡請求失敗時候回調,返回YES再次繼續批量處理
/// @param complete 最終結果回調,返回成功和失敗數據數組
+ (void)HTTPBatchRequestConfiguration:(KJBatchConfiguration *)configuration
reconnect:(KJNetworkBatchReconnect)reconnect
complete:(KJNetworkBatchComplete)complete;
複製代碼
實例化設置配置文件KJBatchConfiguration
,而後傳入咱們須要批量操做的請求體request
,批量結果顯示在complete回調。markdown
關於reconnect回調,這裏說明一下,存在失敗網絡時刻,會先記錄下來而後回調出來,**retuen yes
**則再次請求失敗的網絡。網絡
// 測試批量網絡請求
- (void)testBatchNetworking{
XCTestExpectation * expectation = [self expectationWithDescription:@"test batch."];
NSMutableArray * array = [NSMutableArray array];
{
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.path = @"/headers";
request.responseSerializer = KJSerializerJSON;
[array addObject:request];
}{
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.path = @"/ip";
[array addObject:request];
}{
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.path = @"/user-agent";
[array addObject:request];
}{
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.path = @"/bearer";
request.responseSerializer = KJSerializerJSON;
[array addObject:request];
}{
KJNetworkingRequest * request = [[KJNetworkingRequest alloc] init];
request.method = KJNetworkRequestMethodGET;
request.path = @"/cache";
request.responseSerializer = KJSerializerJSON;
[array addObject:request];
}
KJBatchConfiguration * configuration = [KJBatchConfiguration sharedBatch];
configuration.maxQueue = 3;
configuration.requestArray = array.mutableCopy;
[KJNetworkBatchManager HTTPBatchRequestConfiguration:configuration reconnect:^BOOL(NSArray<KJNetworkingRequest *> * _Nonnull reconnectArray) {
return YES;
} complete:^(NSArray<KJBatchResponse *> * _Nonnull result) {
NSLog(@"----%@",result);
[expectation fulfill];
}];
[self waitForExpectationsWithTimeout:300 handler:nil];
}
複製代碼
具體使用,能夠下載Demo查看測試用例併發
pod 'KJNetworkPlugin/Batch' # 批量插件版網絡請求
複製代碼