Reference document 1

Reference 2

YTKNetwork

The original link

The basic idea of YTK is to encapsulate each network request as an object. Encapsulate each network request into an object pattern known as the design pattern-Command pattern

features

  • You can configure server and CDN addresses in a unified manner

  • Support caching network request content by time

  • Supports sending of interdependent network requests

  • Supports checking the validity of JSON content

YTK source code structure

YTKBaseRequest

YTKRequest’s parent class, which defines the related properties of the Request, Block and Delegate. Default implementation for external interfaces, and common logic.

YTKRequest

The main processing of the cache, update the cache, read the cache, manual write cache, whether to ignore the cache. Archived cache is adopted here. Request mode, root path, request address, request parameters, APP version number, sensitive data stitching and MD5 are used as the file name of the cache to ensure uniqueness. It also provides the setting of the cache storage duration. The main implementation is to determine whether a request is really initiated by comparing the time obtained from the last modification of the cache file with the set cache duration. The following are some logical judgments for initiating a request:

YTKNetworkAgent

The class that really initiates the network request, calls AFN’s method in the addRequest method, this part can conveniently replace the third party library, also includes some request cancellation, plug-in proxy method call, etc., all network request failure or success will call the following method

YTKNetworkConfig

Configure the request root path and DNS address

YTKNetworkPrivate:

Utility class, concatenate address, provide encryption method, define classification, etc.

YTKBatchRequest, YTKChainRequest

The two advanced uses of YKTNetwork, batch network Request and chain network Request, act as a container for holding requests. Define finishedCount to record the number of completed batch requests:

XMNetworking

The original link

XMNetworking is a lightweight, easy-to-use yet powerful networking library based on the AFNetworking 3.0 package.

XMNetworking adopts a centralized design idea. All XMRequest requests are initiated and managed by XMCenter. Information such as callback thread, common Server URL, Header and Parameter can be configured for all requests through XMCenter. At the same time, customized response result processing logic can be realized by Block injection, such as data model transformation, service error code judgment, network cache, etc.

The XMEgine layer was added to isolate the low-level third-party library dependencies so that you can switch to other low-level network libraries or implement the low-level logic yourself

features

  • Simple to use, only need to call a method to send a request, through the Block configuration information, the code is compact;
  • Powerful, applicable to almost all network request scenarios (common request, upload, download)
  • Support batch request, chain request and other complex business logic network requirements;
  • Globally configure common information for all requests, custom callback threads, and response processing logic;

use

Global Network Configuration

[XMCenter setupConfig:^(XMConfig *config) {config.generalServer = @" public server address "; // public headers = @{}; // public request parameter config.generalParameters = @{@"general-parameter": @"general parameter value"}; // Public user info config.generalUserInfo = nil; CallbackQueue = dispatch_get_main_queue(); config.consolelog = YES;}];Copy the code

The initiating

[XMCenter sendRequest:^(XMRequest *request) {// Optional, If empty, read xmCenter.GeneralServer request.server = @"http://example.com/v1/"; request. API = @"foo/bar"; // Request. Parameters = @{@"param1": @"value1", @"param2": @"value2"}; // Optional, default is' POST 'request.httpMethod = kXMHTTPMethodPOST; RequestType = kXMRequestNormal;} onSuccess:^(id responseObject) {NSLog(@"onSuccess: %@", responseObject); } onFailure:^(NSError *error) { NSLog(@"onFailure: %@", error); }];Copy the code

Chain request

If one of these requests fails, the failure Block is immediately executed

[XMCenter sendChainRequest:^(XMChainRequest *chainRequest) { [[[[chainRequest onFirst:^(XMRequest *request) { request.url = @"server url 1";  }] onNext:^(XMRequest *request, id responseObject, BOOL *sendNext) { NSDictionary *params = responseObject;  if (params.count > 0) { request.url = @"server url 2"; request.parameters = params; } else { *sendNext = NO;  } }] onNext:^(XMRequest *request, id responseObject, BOOL *sendNext) { request.url = @"server url 3";  request.parameters = @{@"param1": @"value1", @"param2": @"value2"}; }] onNext: ...]; } onSuccess:^(NSArray<id> *responseObjects) { NSLog(@"onSuccess: %@", responseObjects); } onFailure:^(NSArray<id> *errors) { NSLog(@"onFailure: %@", errors); } onFinished:^(NSArray<id> *responseObjects, NSArray<id> *errors) { NSLog(@"onFinished"); }];Copy the code

The batch request

A batch of requests can be sent at the same time. These requests are logically related but independent of each other. A Success block is executed when all requests are successfully completed, and a failure block is executed when one request fails.

[XMCenter sendBatchRequest:^(XMBatchRequest *batchRequest) { XMRequest *request1 = [XMRequest request];  request1.url = @"server url 1"; XMRequest *request2 = [XMRequest request]; request2.url = @"server url 2";  [batchRequest.requestArray addObject:request1]; [batchRequest.requestArray addObject:request2];  } onSuccess:^(NSArray<id> *responseObjects) { NSLog(@"onSuccess: %@", responseObjects); } onFailure:^(NSArray<id> *errors) { NSLog(@"onFailure: %@", errors); } onFinished:^(NSArray<id> *responseObjects, NSArray<id> *errors) { NSLog(@"onFinished"); }];Copy the code