高級網絡關於鏈式網絡請求

鏈式網絡使用教程

鏈式網絡請求其實主要用於管理有相互依賴的網絡請求,它實際上最終能夠用來管理多個拓撲排序後的網絡請求。git

例如,咱們有一個需求,須要用戶先發送註冊Api,而後獲取用戶信息Api,最後再獲取用戶id等信息。github

鏈式插件方案

  • 方案1:採用自定義參數方式處理
/// 鏈式網絡請求
/// @param request 請求體系
/// @param success 所有成功回調,存放請求全部結果數據
/// @param failure 失敗回調,只要一個失敗就會響應
/// @param chain 鏈式回調,返回下個網絡請求體,爲空時便可結束後續請求,responseObject上個網絡請求響應數據
+ (void)HTTPChainRequest:(__kindof KJNetworkingRequest *)request
                 success:(KJNetworkChainSuccess)success
                 failure:(KJNetworkChainFailure)failure
                   chain:(KJNetworkNextChainRequest)chain,...;
複製代碼
  • 方案2:採用鏈式閉包方式處理
/// 鏈式網絡請求,需 `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));
複製代碼

使用教程

  • 方案1:採用不定參數方式處理

使用說明: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];
}
複製代碼
  • 方案2:採用鏈式閉包方式處理

使用說明: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];
}
複製代碼

大體介紹就差很少這麼多吧,其實這類網絡使用通常都是在我的中心

Cocoapods安裝

pod 'KJNetworkPlugin/Chain'
複製代碼

關於做者

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

相關文章
相關標籤/搜索