IOS development-Ios network programming asynchronous get Method Request Programming

Source: Internet
Author: User

As mentioned in the previous blog, synchronous requests have a poor user experience. In addition, we introduced how to implement Asynchronization in synchronous methods. In fact, ios sdk also provides asynchronous request methods. Asynchronous requests use the nsurlconnection delegation protocol nsurlconnectiondelegate. The delegate object method is called back at different stages of the request. The nsurlconnectiondelegate Protocol has the following methods:

Connection: didreceivedata: the request is successful and starts to receive data. If the data volume is large, it will be called multiple times;

Connection: didfailwitherror: An exception occurred when loading data;

Connectiondidfinishloading: The data is successfully loaded and executed after the connection: didreceivedata method;

The masterviewcontroller. H code for the master View Controller Using asynchronous requests is as follows:

# Import <uikit/uikit. h> # import "nsstring + urlencoding. H "# import" nsnumber + message. h "@ interface masterviewcontroller: uitableviewcontroller <nsurlconnectiondelegate> @ property (strong, nonatomic) detailviewcontroller * detailviewcontroller; // Save the data list @ property (nonatomic, strong) nsmutablearray * listdata; // receives the data returned from the slave server. @ Property (strong, nonatomic) nsmutabledata * datas; // reload the table view-(void) reloadview :( nsdictionary *) RES; // start to request the Web Service-(void) startrequest; @ end

 

 

The above code implements the nsurlconnectiondelegate protocol in the masterviewcontroller definition. The datas attribute is used to store the data returned from the server and is defined as a variable type. It is used to continuously append data to this datas during the loading of data from the server. The masterviewcontroller. m code is as follows:

/** Start to request Web Service */-(void) startrequest {nsstring * strurl = [[nsstring alloc] initwithformat: @ "http: // iosbook3/mynotes/WebService. php? Email =%@ & type =%@ & Action =%@ ", @" <your iosbook1.com user email address> ", @" JSON ", @" Query "]; nsurl * url = [nsurl urlwithstring: [strurl urlencodedstring]; nsurlrequest * request = [[nsurlrequest alloc] Protocol: url]; nsurlconnection * connection = [[nsurlconnection alloc] initwithrequest: requestdelegate: Self]; If (connection) {_ datas = [nsmutabledata new] ;}# Pragma mark-nsurlconnection callback method-(void) connection :( nsurlconnect Ion *) connection didreceivedata :( nsdata *) Data {① [_ datas appenddata: Data];}-(void) connection :( nsurlconnection *) connection didfailwitherror: (nserror *) error {nslog (@ "% @", [error localizeddescription]);}-(void) connectiondidfinishloading: (nsurlconnection *) connection {② nslog (@ "request completed ..."); Nsdictionary * dict = [nsjsonserialization jsonobjectwithdata: _ datasoptions: nsjsonreadingallowfragments error: Nil]; [self reloadview: dict];}

 

In the connection: didreceivedata: Method of row ①, it is very important to understand this point by continuously receiving data from the server through the [_ datas appenddata: Data] statement. If the load is successful, the connectiondidfinishloading of row ② will be called back. This method also means the end of the request. At this time, the data in _ datas is complete, here, the data is sent back to the View Controller of the presentation layer.

 

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.