IOS synchronous request and asynchronous request

Source: Internet
Author: User
Tags call back

After class, I sat down and quietly summarized what I learned today.
First, synchronous requests and asynchronous requests

Synchronous request,
Using the main thread to obtain all the request data at a time leads to a relatively easy problem.
When the requested data is large, a choppy phenomenon occurs, that is, the main thread is blocked.
Poor User Experience

Asynchronous request
An asynchronous request is to create another thread to request data and request data gradually,
In this way, you can continuously obtain data and then update the interface, so as not to cause freezing.


Let's take a request for a URL link as an example.

Synchronous request
// Define a macro
# Define kURL @ "http://www.baidu.com"

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
Self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease];
// Override point for customization after application launch.
Self. window. backgroundColor = [UIColor whiteColor];

// Create a URL
NSURL * mURL = [NSURL URLWithString: kURL];

// Create a request with a maximum request time of 20 seconds
NSURLRequest * requrst = [NSURLRequest requestWithURL: mURL cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 20];

// Synchronize the parameters returned by the request
NSURLResponse * response = nil;
NSError * error = nil;
// Establish a connection, download data, and synchronize requests
NSData * data = [NSURLConnection sendSynchronousRequest: requrst returningResponse: & response error: & error];

// Print the data returned by the server
NSLog (@ "data = % @", data );
// Print the error message when a request error occurs
NSLog (@ "error is % @", [error localizedDescription]);

[Self. window makeKeyAndVisible];
Return YES;
}



The following describes asynchronous requests in the same situation.
First, follow the protocol NSURLConnectionDataDelegate.

-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
Self. window = [[[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds] autorelease];
// Override point for customization after application launch.
Self. window. backgroundColor = [UIColor whiteColor];

// A URL
NSURL * pURL = [NSURL URLWithString: kURL];

// Create a request
NSURLRequest * Pequest = [NSURLRequest requestWithURL: pURL cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 20];
// Initiate a request and call back in the delegate Mode
[NSURLConnection connectionWithRequest: Pequest delegate: self];

[Self. window makeKeyAndVisible];
Return YES;
}


// The method in the Implementation Protocol is as follows:


# Response from The pragma Server
// Server response callback
-(Void) connection :( NSURLConnection *) connection didReceiveResponse :( NSURLResponse *) response
{
NSLog (@ "server response ");
// Create an empty data
Self. mData = [NSMutableData data];

}


// The data returned by the server is the returned data. Note that not all data is returned at a time.
-(Void) connection :( NSURLConnection *) connection didReceiveData :( NSData *) data
{
NSLog (@ "server returned data ");
[Self. mData appendData: data];
}


// The data has been received
-(Void) connectionDidFinishLoading :( NSURLConnection *) connection
{
NSLog (@ "data received ");
// Print the received data
NSLog (@ "data = % @", self. mData );
}


// Method called for data failure
-(Void) connection :( NSURLConnection *) connection didFailWithError :( NSError *) error
{
NSLog (@ "failed to receive data, cause of failure % @", [error localizedDescription]);
}


This is an asynchronous request. You can update the interface information with the accepted data while accepting the data, so you do not have to accept the data.
To update the interface.


The above are synchronous requests and asynchronous requests.


Summary is not complete. Go to the next article about the two request methods for the server: GET and POST

Link: http://blog.csdn.net/lc_obj/article/details/17604539


Write to yourself-LC


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.