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.