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