Introduction to ASI Usage (synchronous vs. asynchronous)

Source: Internet
Author: User

ASI is asihttprequest. official website: http://allseeing-i.com/ASIHTTPRequest/. Can be downloaded from the above to the latest source, as well as to obtain relevant information.

Using the HTTP Network request API in the iOS SDK is quite complicated and cumbersome to call, ASIHTTPRequest is a set of APIs that encapsulates the Cfnetwork API and is very simple to use, written in Objective-c, can be very well applied in Mac OS x systems and iOS platform applications. ASIHTTPRequest applies to basic HTTP requests, and to the interaction between rest-based services. Light say do not practice false bashi, the following several chapters I will introduce the following ASI use occasions. In this section I describe the concepts and use of synchronous and asynchronous.


Concepts of synchronization and Asynchrony:

synchronization: When a function call is made, the call does not return until the result is obtained. that is, one thing must be done. To do the next thing when the previous one is done. .

Async: When an asynchronous procedure call is made, the caller cannot get the result immediately. The part that actually handles the call notifies the caller via status, notification, and callback after completion.


Simultaneous use of ASI

Sync request-(void) sync{    nsurl *url = [Nsurl urlwithstring:@ "http://www.baidu.com"];    Requested request    self.request = [ASIHTTPRequest requestwithurl:url];    Set Request Timeout length    self.request.timeOutSeconds = 5;    Start Synchronous request Operation    [Self.request startsynchronous];        if (self.request.error) {        NSLog (@ "error:%@", [Self.request.error localizeddescription]);    }    else{        //Response related Information        NSLog (@ "Return code:%d, Return message:%@", Self.request.responseStatusCode, Self.request.responseStatusMessage);                NSLog (@ "Return string:%@, Return message:%@", Self.request.responsestring,self.request.responsedata);}    }

When the code executes to

[Self.request startsynchronous];
this sentence, the thread will be blocked, that is, the subsequent code can not execute. You must wait for the network data to return before you can continue to execute the following code. This is also a feature of synchronous requests.


Asynchronous use of ASI (implement asihttprequestdelegate)

Asynchronous request-(void) Async {    Nsurl *url = [Nsurl urlwithstring:@ "http://www.youku.com"];    Self.request = [ASIHTTPRequest requestwithurl:url];    Self.request.timeOutSeconds = 5;    Self.request.delegate = self;        [Self.request startasynchronous];}
Proxy Method

-(void) requestfinished: (ASIHTTPRequest *) Request {    //response related Information    NSLog (@ "Return code:%d, Return message:%@", Request.responsestatuscode,request.responsestatusmessage);    NSLog (@ "Return string:%@, Return message:%@", Request.responsestring,request.responsedata);} -(void) requestfailed: (ASIHTTPRequest *) request{}


the asynchronous use of ASI (block mode )

Asynchronous request (block mode)-(void) async{    nsurl *url = [Nsurl urlwithstring:@ "http://www.youku.com"];    Self.request = [ASIHTTPRequest requestwithurl:url];    Self.request.timeOutSeconds = 5;        [Self.request setstartedblock:^{        NSLog (@ "Begin");    }];        __weak typeof (Self.request) that = Self.request;    [Self.request setcompletionblock:^{        NSLog (@ "result:%@", [NSString stringwithformat:@ "Data string:%@", That.responsestring]);    }];        [Self.request startasynchronous];}

Two points to note about asynchronous requests

1. Request this variable should be a strong reference work, because after the execution of the method async, Self.request This object is destroyed. The problem does not occur in the synchronization request, because the Self.request object is destroyed when the response data is received, but the object is lost when the asynchronous request is made, and the final response data is not obtained. The request property is defined as follows:

@property (Nonatomic,strong) asihttprequest *request;


2. When the controller object is destroyed, the request object should be destroyed! Because when a request is issued, perhaps 5 seconds after the response, no wait for the response back when you switch the controller, then produced a wild pointer!!! so be sure to call the following code when the controller is destroyed!!

-(void) dealloc {    [self.request cleardelegatesandcancel];}

Introduction to ASI Usage (synchronous vs. asynchronous)

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.