iOS Development-afnetworking

Source: Internet
Author: User

I. Request through Afhttpsessionmanager

1. Create Manager

Afhttpsessionmanager *manager = [Afhttpsessionmanager manager];

2. Set timeout time, receive type

Ten ;     = [Nsset setwithobjects:@ "application/json"@ "text/html", @" Text/json "@" Text/javascript"@"text/plain " , nil];

3. Initiating an HTTP request

ID   _nullable responseobject) {       //Successful code     } failure:^ (Nsurlsessiondatatask * _ Nullable task, Nserror * _nonnull error) {       //Failed code     }];

Second, afnetworking HTTP-related classes

Afhttpsessionmanager: subclass of the Afurlsessionmanager class that contains many settings and methods for HTTP; 

Afhttprequestserializer Nsurlrequest's constructor

Afhttpresponseserializer Nsurlresponse's constructor

Third, the process

1. Call the POST request

-(Nsurlsessiondatatask *) POST: (NSString *) urlstring                    parameters: (ID) parameters                      Progress: (void (^) (nsprogress * _nonnull)) uploadprogress                       success: (voidID  _nullable)) Success                       failure: (void (^) (Nsurlsessiondatatask * _nullable, Nserror * _ nonnull)) Failure

2. Create a new task

-(Nsurlsessiondatatask *) Datataskwithhttpmethod: (NSString *) method URLString: (NSString*) urlstring parameters: (ID) Parameters Uploadprogress: (Nullablevoid(^) (Nsprogress *uploadprogress)) Uploadprogress downloadprogress: (Nullablevoid(^) (Nsprogress *downloadprogress)) DownloadProgress Success: (void(^) (Nsurlsessiondatatask *,ID)) Success failure: (void(^) (Nsurlsessiondatatask *, Nserror *)) Failure

3. Create a new request

-(Nsmutableurlrequest *) Requestwithmethod: (NSString *) method                                 *) urlstring                                parameters: ( ID ) Parameters                                     *__autoreleasing *) error

4. Create a new Datatask

-(Nsurlsessiondatatask *) Datataskwithrequest: (Nsurlrequest *) request                               void (^) (nsprogress * uploadprogress)) Uploadprogressblock                             void (^) (nsprogress *downloadprogress)) Downloadprogressblock                             void ID _nullable responseobject,  nserror * _nullable error)) Completionhandler

5. Add delegate

-(void) Adddelegatefordatatask: (Nsurlsessiondatatask *) datatask                void (^) ( Nsprogress *uploadprogress)) uploadprogressblock              void (^) (nsprogress *downloadprogress)) Downloadprogressblock             Completionhandler: (voidID responseobject, nserror *error)) Completionhandler

IV. Overall structure

The Mutabletaskdelegateskeyedbytaskidentifier attribute of the 1,afurlsessionmanager , which stores each task corresponding to a Afurlsessionmanagertaskdelegate;

2,afurlsessionmanager's nsurlsessiondelegate Implementation, will call each task corresponding to the afurlsessionmanagertaskdelegate;

3, set the task Afurlsessionmanagertaskdelegate, use the Nslock, multi-threaded synchronization;

The scheduling of tasks takes the application of Factory mode, observer and proxy mode.

Factory mode

For HTTP protocol requests there are Afhttprequestserializer,afjsonrequestserializer, Afpropertylistrequestserializer classes, Three are dependent on the abstract interface afurlrequestserialization

The response to the HTTP protocol has

Afhttpresponseserializer, Afjsonresponseserializer, Afxmlparserresponseserializer, Afxmldocumentresponseserializer, Afpropertylistresponseserializer, Afimageresponseserializer, Afcompoundresponseserializer

are dependent on the abstract interface afurlresponseserialization

In line with the open-closed principle

To add a new request header, the response header is open, and add new content does not need to modify the original content

Observer pattern

For each life cycle of a task, there will be a corresponding event emitted

Afnetworkingtaskdidresumenotification

Afnetworkingtaskdidsuspendnotification

for the task to listen in KVO way Progress

Proxy Mode

Afurlsessionmanagertaskdelegate is the logic encapsulation of AF to itself, while achieving nsurlsessiontaskdelegate, nsurlsessiondatadelegate, nsurlsessiondownloaddelegate

at the same time by implementing Nsurlsessiondelegate,

Flow chart

iOS Development-afnetworking

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.