Synchronous download and asynchronous download for iOS Network Development

Source: Internet
Author: User

1. synchronous download (poor interaction, prone to freezing, usually used only when downloading data is small or has specific requirements ). After a synchronous request is sent, the program stops user interaction until the server returns data.

Steps:

1. Create an nsurl

Nsurl * url = [[nsurl alloc] initwithstring: @ "http://www.baidu.com/"];

 

2. Create nsurlrequest through URL

Nsurlrequest * request = [[nsurlrequest alloc] initwithurl: URL cachepolicy: nsurlrequestuseprotocolcachepolicy timeoutinterval: 15];

 

Cachepolicy Cache Protocol is an enumeration type:

Nsurlrequestuseprotocolcachepolicy Basic Policy


Nsurlrequestreloadignoringlocalcachedata ignore local cache

Nsurlrequestreturncachedataelseload first uses the cache. If there is no local cache, It is downloaded from the original address.


Nsurlrequestreturncachedatadontload uses the local cache and never downloads it. If the local cache does not exist, the request fails. This policy is mostly used for offline operations.


Nsurlrequestreloadignoringlocalandremotecachedata ignores any Cache Policy. Whether it is local or remote, it is always downloaded from the original address.


Nsurlrequestreloadrevalidatingcachedata is not downloaded if the local cache is valid. Download again from the original address in any other situation

 

 

3. Establish a network connection nsurlconnection to synchronize request data

Nsdata * receiveddata = (nsmutabledata *) [nsurlconnection sendsynchronousrwquest: Request returningresponse: & response error: & error];

 

After the preceding three steps, you need to parse the receiveddata, which is generally XML/JSON.

 

Ii. asynchronous download

 

Steps:

The first two steps are the same as the synchronization method. When receiving data in step 3, they are different from the synchronization method. Four network connection proxy methods are required.

1. Create an nsurl

Nsurl * url = [[nsurl alloc] initwithstring: @ "http://www.baidu.com/"];

 

2. Create nsurlrequest through URL

Nsurlrequest * request = [[nsurlrequest alloc] initwithurl: URL cachepolicy: nsurlrequestuseprotocolcachepolicy timeoutinterval: 15];

 

3. Establish a network connection nsurlconnection and set its proxy

[Nsurlconnection connetionwithrequest: Request delegate: Self];

Proxy to be implemented:

Nsurlconnectiondatadelegate, nsurlconnectiondelegate

The following four proxy methods are used:

 

// This method is called when receiving server responses

-(Void) connetion :( nsurlconnetion *) connection didreceiveresponse :( nsurlresponse *) Response

{

// Initialize receivedata, which is used to store data from the server

Self. receiveddata = [nsmutabledata data];

}

 

// Called when receiving data transmitted by the server. This method is executed several times based on the data size.

-(Void) connection :( nsurlconnection *) connetion didreceivedata :( nsdata *) data

{

[Self. receiveddata appenddata: Data];

}

 

// Call this method after data transmission.

-(Void) connetiondidfinishloading :( nsurlconneciton *) connetion

{

// Todo get the final receiveddata


}
 

// Any errors, such as network disconnection and connection timeout, occur during the network request process.

-(Void) connetion :( nsurlconnection *) connetion didfailwitherror :( nserror *) Error

{

Nslog (@ "% d @", [error localizeddescription]);

}
 

 

 

 

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.