Brief Introduction to implementation analysis and simple use of AFN (AFNetWorking 2.X), afnafnetworking

Source: Internet
Author: User

Brief Introduction to implementation analysis and simple use of AFN (AFNetWorking 2.X) [conversion], afnafnetworking

[Reprinted]

Axc Note: if the version is AFNetWorking 2.6 3.0, You need to modify most objects.

I. Statement
Previously, this person used the ASI network request class library. I recently started to learn about using the AFNetworing class library. There will certainly be a better introduction to AFN's blog articles on the Internet. I hope you can read them in a dialectical manner and get the best of them to get rid of them. My understanding may be wrong or incomplete. Please do not correct me.
Blog Source: http://386502324.blog.163.com/blog/static/1134693772015522115237696/

Ii. Summary
In cocospod, AFNetworing contains all classes except several categories. The header file AFNetworking. h of the class library introduces all the following class libraries, and you can use different implementation methods based on different systems. The following describes the general functions of each class, mainly by translating comments to API documents.

1: AFURLConnectionOperation
It can be said that it is the most basic class of AFN. Inherits from the NSOperation class and attaches network requests to an operation. This allows us to effectively control and observe the problems and statuses of a network request, such as creation, progress, cancellation, completion, pause and recovery, and exceptions. [Part 3: detailed analysis of implementation and functions]

2: AFHTTPRequestOperation
A subclass of AFURLConnectionOperation for HTTP or HTTPS requests. It encapsulates an acceptable status code and content type, and determines whether a request result is successful or failed.
In fact, the system adds several parameters required for HTTP network requests.

3: AFHTTPRequestOperationManager
This class is the core class of the AFN class library. It encapsulates a general mode that helps us easily and conveniently create requests and serialize responses, network status monitoring, security policies, and operation management for each request (operation dependency or status change ).
[Part 3: detailed analysis of implementation and functions]

4: AFURLSessionManager
After iOS7, Apple added a new network request class-NSURLSession. AFN officially recommends iOS 7 or Mac OS X 10.9 or above. It is best to use this class to initiate network requests to replace AFHTTPRequestOperationManager. However, based on iOS6, which is currently the most suitable for most domestic apps, this category is not widely used. The description or usage of NSURLSession is not described in detail. View the API documentation. You will have time to add this class.

5: AFURLSessionManager
Inherited from AFURLSessionManager. Similar to the relationship between 1 and 2. It also facilitates the use of HTTP and https requests, and adds some interfaces for convenient calls.

6: AFNetworkReachabilityManager
Network Connection status monitoring and network type. The Reachability class name officially provided by Apple is replaced with the well-known class name to prevent conflicts with the well-known class name provided by the system.

7: AFSecurityPolicy
I don't know much about this. It's a security policy class. Generally, it does not seem to be available. google is required ..

8: AFURLRequestSerialization
①: The object that complies with this protocol is used to process the request. It converts the request parameter to the query string or entity body format and sets the necessary header.
②: Construct a multipart request.

9: AFURLResponseSerialization
Objects that follow the AFURLResponseSerialization protocol are used to verify, serialize responses and related data and convert them into useful forms, such as JSON objects, images, and even mantle-based model objects.

Iii. Implementation analysis of key classes

1: Implementation of AFURLConnectionOperation

①: Summary
AFURLConnectionOperation combines Operation and URLConnection, and uses operation to listen to the status and establish dependency between each other to implement perfect control over a NSURLConnection object, and return the request result through block-friendly.

②: Implement file. m
We will summarize the main methods of this class in. m.
1: first we can see that it creates a singleton thread. This thread will be resident in the memory to process all request tasks initiated by AFN. Of course, the thread also follows a runloop, and AFN sets the runloop mode to NSDefaultRunLoopMode. NSDefaultRunLoopMode cannot detect the connection state. This shows that AFN will not process the UI Refresh after connection is completed in this thread, but will throw the data to the main thread so that the main thread can refresh the UI.
2: we can see that the URLRequest and NSURLConnection objects are created by receiving the request string. Request.
3: The lock is applied to files multiple times to ensure data security. Of course, he also implemented several data NSCoping protocols.
4: Control of issues and status such as request creation, progress, cancellation, completion, pause and recovery, and exceptions. Here we will talk about pause and recovery.
The temporary stop actually cancels the network request. However, since the nscoping protocol is implemented, the downloaded data can be saved. Next time we make the same request, we will send the downloaded data node together to the server, telling the server that the data of these departments is not needed, the server can return the corresponding data to me based on the return node I sent. This enables pause and recovery, that is, resumable upload.
5: rewrite the operation method. Google on your own. I will not go into details here.
6: Implementation of various status control methods and notification of status changes

③: Interface file. h
The attribute methods in the interface document can be summarized as follows:
1: read-only data, allowing managers to receive it.
2: Set the modes of the runloop. The default defaultmodes of the class library is no longer used.
3: Status Control Method
4. Security Policy Settings
All in all, the interfaces exposed by the interface file. h are used to allow the manager to fully control the operation and network requests.

2: AFHTTPRequestOperationManger

①: Summary
This class is the core class of the entire class library. It is said that all network request-related settings were mixed into a client before AFN2.0, resulting in a particularly bloated client. After 2.0, AFN extracts some settings and threads specialized classes [AFSecurityPolicy, AFURLRequestSerialization, AFURLResponseSerialization ]. Now, the overall design of AFN is perfect. Coupling has become very low, and some problems in version 1.0 have also been improved.

②: Implement file. m
The implementation file is relatively simple. You can see that it creates a queue. And add each operation to the queue. In the queue, you can set dependencies and concurrency for each request.

③: Interface file. h
Interface file. This class can be used to set parameters such as AFSecurityPolicy, AFURLRequestSerialization, and AFURLResponseSerialization. This is the way to reduce coupling. It's easy to use. I will not go into details here.

Iv. Supplement
Recently, people were in a hurry and also fell. The description is not very detailed, and modifications will be added in the future. The implementation of the Seven, eight, and nine classes will be added.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.