First of all, the basic component of the network layer should be AFNetworking. Most developers are based on the secondary encapsulation of AFNetworking, and the well-known ones are CTNetWork and YTKNetwork. After reading the source code, I think it does not meet our actual business needs. There should also be the following features

  1. Simple, easy-to-understand code
  2. Log output
  3. Cache lru algorithm should be used as a developer is not clear specific like what content, users can only make important interface of all the buffer, however, the user may be a time based on curiosity will click on most of the APP interface, and caused a large number of buffer, the waste of resources is not necessary, so the individual feels to clear cache of shooting is not high
  4. Canceling a network request

Now let’s talk about CTNetworking and YTKNetwork


In Casa Taloyum’s content article on network layer design solutions for iOS application Architecture, the two main issues discussed are block and proxy intensive and discrete technical points

Casa TaloyumThe disadvantages of block in network layer are discussed

  • Blocks are hard to track and hard to maintain
  • Blocks extend the life of the object in question
  • Block does not comply with the usage specifications in discrete scenarios

Problems with intensive models

  • Cause 1: When the current request is flying outside, there are two different take-off strategies according to different service requirements. One is to cancel the newly initiated request and wait for the request to land. The other is to cancel the outside requests and let the new ones take off. Intensive API calls that meet this need require more judgment and cancellation code each time they are called, which is not very clean.
  • Reason 2: It’s easy to AOP on an API request. In the case of intensive API calls, how would the code be written if you were to AOP the takeoff and landing process of an API request… Oh, my god, this is so beautiful that I can’t even think about it.

  • Advantages of discrete type

  • Reason 3: When the API request’s landing point disappears, the discrete API call approach can handle the situation more transparently.
  • Reason 4: Discrete API calls, such as the Reformer mechanism based on discrete API calls, provide maximum flexibility to the business side. In addition, if you are targeting an API that provides a page-turning mechanism, APIManager can simply provide itloadNextPageMethod to load the next page, page number management is not managed by the business side. Also, if you want to validate business request parameters, such as the user filling in registration information, it’s very easy to implement in a discrete APIManager.

The author thinks that “some short-scale inch A director” everyone has his strengths and weaknesses, may be in one kind of environment, the advantages of A B A bit more of one kind of environment, this problem is relative, for example in computer, either in space, in time, or time in space, and fish and bear’s paw.

Block advantages:

  • Saves a lot of code for writing agents
  • 2. Blocks are lighter and easier to use. They can access the context directly, so there is no need to store temporary data in the class

Block faults:

  • 1. Blocks are not secure enough. If a block is used, a circular reference will be formed, causing the object to be released. Such circular references are difficult to detect once they occur.
  • 2. Block efficiency is low. Data used in block removal from stack memory is copied to heap memory 3. Blocks are not intuitive or easy to maintain when communicating with multiple events.

The delegate advantage:

      

2. Delegate is efficient. A delegate simply stores a pointer to an object

3. The delegate is intuitive and easy to maintain when communicating with multiple events.


The delegate faults:

  • 1. Because the method declaration and implementation are separated, the code is not very consistent and no block is easy to read
  • 2. In many cases, some temporary data needs to be stored ————

The author advocates lighter, simpler and more coherent codes, so the author uses Block when encapsulating SJNetwork. Buddhism says: Only when you give up, you can gain.


indulge_inBig God thinks CTNetworking is inadequate:


  • Use IOP mode to establish modules, inheritance into combination. independent<CTServiceProtocol>and<CTAPIManagerInterceptor>As a part of the intensive management protocol, if some interfaces need to modify these common configurations, they can only be determined in the intensive management module, which is a bit complicated.

  • Dealloc automatically cancels network requests that have not yet landed. In fact, network request tasks hold the request, so the automatic cancellation policy is not valid.





YTKNetwork inadequate:


  • Based on polymorphic design ideas, provides many methods for overload, from the design point of view, the framework can be instantiatedYTKBaseRequestIf subclasses are used directly, then you can’t override these methods to customize them. And, when a reqeust many timesstartThese overloaded methods are called multiple times when a request is made, potentially causing extra computation;

  • The cache policy uses oneYTKBaseRequestA subclass ofYTKRequestAlthough it may seem elegant that the parent class and subclass have a single responsibility, caching policies inevitably change the logic of the parent class, making it difficult not to violate the open and close principle. The framework cache has only one expiration time control, which I found to be too much to change when TRYING to expand.

  • When the same request instance invokes network requests multiple times (multiple network requests are concurrent), the actual processing policy is not made and only the latest one is reservedNSURLSessionTaskWhile on the old unfinished allNSURLSessionTaskLoss of control.

  • The network request task strongly holds all request objects. In the weak network environment, a large number of request objects may not be released, and the interface landing point may not exist.


CTNetworking


Better readability than YTKNetwork

CTJsbridge already interacts with CTNetworking, and H5 engineers can easily use CTNetworking based networking apis.

Various error error types of callbacks are more complete

YTKNetwork

I think the readability of YTKNetwork is not as good as that of CTNetworking. In my opinion, no matter what design mode is used, the code logic is clearer and the code is easier to understand and read. If you don’t agree with me, please ignore it.

The log output, buffering and cancellation of network requests of YTKNetwork and CTNetworking should also be encapsulated three times when we use them. In my opinion, a network layer design should be encapsulated and then provided for developers to call.


SSJNetWork encapsulates buffering, logging, and automatic network requests.

1. Cache processing

The cache-handling configuration is in SJNetWorkConfig and SJNetworkRequestConfig classes and supports the following configurations:

  • – Memory/disk storage mode
  • – Valid duration of cache
  • – Determine whether a cache is needed based on the request shouldAllIgnoreCache
  • – And directly configure YYCache
  • – Support the maximum number of buffers (using YYCache LRU algorithm)
  • – Cache version

2. Automatically cancel the network request

Use AOP hook to automatically cancel the network request

swizzling_exchangeMethod([self class],@selector(popViewControllerAnimated:), @selector(ssj_popViewControllerAnimated:));  swizzling_exchangeMethod([self class],@selector(popToRootViewControllerAnimated:), @selector(ssj_popToRootViewControllerAnimated:)); swizzling_exchangeMethod([self class],@selector(popToViewController:animated:), @selector(ssj_popToViewController:animated:)); swizzling_exchangeMethod([self class],@selector(dismissViewControllerAnimated:completion:), @selector(ssj_dismissViewControllerAnimated:completion:));Copy the code

Set className in SJNetworkRequestConfigz. If no parameter is passed, the network request is automatically invalid

The automatic cancellation of network requests is designed to cancel all network requests under the current VC when the view POP and dismiss


3. Log output, using part of the code NetworkEye can monitor all HTTP requests in the App and display all information related to the request, which is convenient for the network debugging of App development

SJNetWorkConfig ‘variable configuration,

DubugLogeEnable: Request completion console direct output

SQLLogEnable: record when SQL improves jump to VC display

Ne_sqlitePassword: indicates the log database password

Ne_saveRequestMaxCount: Maximum number of requests saved


Source address please click here, welcome god guidance, if the place is not considered, please give more comments