Afnetworking Source Details (ii)

Source: Internet
Author: User

Source: Yuzeyang

Links: http://zeeyang.com/2016/03/15/AFNetWorking-two/

Afhttpsessionmanager inherits from the Afurlsessionmanager, providing a more convenient method of HTTP request, including the GET, POST, PUT, PATCH, delete five ways, And AF encourages us to do another package at Afhttpsessionmanager to meet our own business needs.

At the beginning, AF has been reminded of an attribute BaseURL, which you can use to write BaseURL as your own HTTP request original address in further encapsulation, such as

+ (nsurl *)BaseURL {

return [nsurl urlwithstring: kbaseurlstring];

}

When stitching the baseurl, there are a few points to keep in mind, to prevent the URL of the request from appearing.

Nsurl *baseurl = [nsurl urlwithstring:@ "http://example.com/v1/"];

[nsurl urlwithstring:@ "foo" relativetourl: BaseURL]; //Http://example.com/v1/foo

[nsurl urlwithstring:@ "Foo?bar=baz" relativetourl: BaseURL]; //Http://example.com/v1/foo?bar=baz

[nsurl urlwithstring:@ "/foo" relativetourl: BaseURL]; //Http://example.com/foo

[nsurl urlwithstring:@ "foo/" relativetourl: BaseURL]; //Http://example.com/v1/foo

[nsurl urlwithstring:@ "/foo/" relativetourl: BaseURL]; //http://example.com/foo/

[nsurl urlwithstring:@ "http://example2.com/" relativetourl: BaseURL]; //http://example2.com/

In the initialization method, we see this method

- (instancetype)initwithbaseurl:(nullable nsurl *)URL;

- (instancetype)initwithbaseurl:(nullable nsurl *)URL

Sessionconfiguration:(nullable nsurlsessionconfiguration *)configuration Ns_designated_initializer;

What is the role of Ns_designated_initializer?

The specified constructor guarantees that the object is fully initialized by sending an initialization message to the parent class, specifying that the constructor has several rules:

1. Specifies that the constructor must call the specified constructor of the parent class

2. Any one of the convenience constructors must call another constructor that eventually points to the specified constructor

3. A class with a specified constructor must implement all the specified constructors of the parent class

- (instancetype)init {

//Point-[Initwithbaseurl:]

return [self initwithbaseurl: nil];

}

- (instancetype)initwithbaseurl:(nsurl *)URL {

//Point-[initwithbaseurl:sessionconfiguration:]

return [self initwithbaseurl: URL sessionconfiguration: nil];

}

- (instancetype)initwithsessionconfiguration:(nsurlsessionconfiguration *)configuration {

//Point-[initwithbaseurl:sessionconfiguration:]

return [self initwithbaseurl: nil sessionconfiguration: Configuration];

}

- (instancetype)initwithbaseurl:(nsurl *)URL

Sessionconfiguration:(nsurlsessionconfiguration *)configuration

{

//Call the parent class-[initwithsessionconfiguration:]

Self = [super initwithsessionconfiguration: Configuration];

if (! Self) {

return nil;

}

//BaseURL assignment, Afhttprequestserializer and Afjsonresponseserializer serialization

return self ;

}

Deprecated_attribute This believe that everyone is more, literally means this API is not recommended for developers to use, and then use, there will be a compilation warning

- (nullable nsurlsessiondatatask *)GET:(nsstring *) URLString

Parameters:(nullable ID)parameters

                       success : ( nullable void ( ^ ) ( nsurlsessiondatatask *task , ID _nullable responseobject ) success

Failure:(nullable void (^) (nsurlsessiondatatask * _nullable Task , nserror *error))failure Deprecated_attribute;

The following post, GET, PUT, PATCH, delete methods are basically similar

URLString indicates that the requested url,parameters represents the memory of the client requesting the content, progress represents the progress of the request, and Constructingbodywithblock there is only one formdata used to splice to the HTTP request body , success represents the block callback after the request succeeds, and failure indicates that the request failed block callback

So what's the difference between these few requests?

1, POST request is to send data to the server, to update the resource information, it can change the type of data and other resources

2, GET request is to the server to initiate the request data, to obtain or query resource information

3, the put request and the POST request is very similar, all sends the data, but the put request cannot change the data the kind and so on resources, it can only modify the content

4. Delete request is used to delete a resource

5, patch request and put request is also used for data update, it is recommended for update HTTP verb

In the actual development process, we still use post and get request is the most

In the part of the request implementation, it is called-[datataskwithhttpmethod:urlstring:parameters:uploadprogress:downloadprogress:success:failure] method to create a Nsurlsessiondatatask object

The content of the parameters is basically the same as the previous one, and method refers to the type of the request.

- (nsurlsessiondatatask *)Datataskwithhttpmethod:(nsstring *) method

URLString:(nsstring *)urlstring

Parameters:(ID)parameters

Uploadprogress:(nullable void (^) (nsprogress *uploadprogress)) uploadprogress

DownloadProgress:(nullable void (^) (nsprogress * DownloadProgress)) downloadprogress

                                           Success : ( void ( ^ ) ( Nsurlsessiondatatask * , ID ) success

Failure:(void (^) (nsurlsessiondatatask *, nserror * ))failure

{

nserror *serializationerror = nil;

nsmutableurlrequest *request = [self. Requestserializer Requestwithmethod: Method urlstring:[[nsurl urlwithstring : URLString relativetourl: Self. BaseURL] absolutestring] parameters:p arameters error:& Serializationerror];

if (serializationerror) {

if (failure) {

#pragma clang diagnostic push

#pragma clang diagnostic ignored "-WGNU"

Dispatch_async(self. Completionqueue ?: Dispatch_get_main_queue(), ^{

failure(nil, serializationerror);

});

#pragma clang diagnostic pop

}

return nil;

}

__block nsurlsessiondatatask *datatask = nil;

datatask = [self datataskwithrequest: Request

Uploadprogress: Uploadprogress

DownloadProgress:d ownloadprogress

Completionhandler: ^(nsurlresponse * __unused response, ID Responseobject, nserror *error) {

//Failure to successfully process

}];

return datatask;

}

Afnetworking Source Details (ii)

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.