高級網絡關於批量網絡請求

批量網絡使用教程

關於批量網絡請求,平時使用的場景是很是之多,因而乎就封裝該批量插件版網絡請求,下面介紹一下使用技巧。git

這裏主要分爲兩個大類:github

  • **KJBatchConfiguration:**配置文件,這裏包括網絡請求最大併發數,最大失敗重連次數,失敗重連時機和批量網絡請求體
/// 設置網絡併發最大數量,默認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;
複製代碼
  • **KJNetworkBatchManager:**批量網絡請求管理器

API說明

批量插件網絡請求,這裏提供設置最大併發數量,失敗調用次數,錯誤重連時機等配置信息數組

/// 批量網絡請求
/// @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查看測試用例併發

Cocoapods安裝

pod 'KJNetworkPlugin/Batch' # 批量插件版網絡請求
複製代碼

關於做者

救救孩子吧,謝謝各位老闆~~~~

相關文章
相關標籤/搜索