Site icon Experience, Digital Engineering and Data & Analytics Solutions by Apexon

iOS: Unrevealed Better way of Communication

Testing

Apart from few games and static utility applications, most of the iPhone applications communicate with the remote server. As long as the payload is limited to few kilobytes and generation time of payload is low, traditional communication methods give acceptable performance.

 

Generally, developers send data to the server after entire data is collected from the end user. Better approach is to send the data into chunks to the server. Data sent into chunks can be regenerated as a single payload on receiver end or it can be processed into chunk format also. This mode of communication is known as Chunked Transfer. Significant performance gain can be achieved when data is streamed to the server, a heavy payload needs to be uploaded to the server and when data generation time on client side is more.

 

There are no major changes required on server end to take the advantage of chunked transfer. Chunked transfer is a data transfer technique of HTTP version 1.1. If the http header contains the value as “chunked” for key “Transfer-Encoding”, it will ignore the “Content-Length”. It informs HTTP that the size of the payload is not known yet. Once the end chunk is identified, the data transfer is terminated.

 

Lets understand, the code changes needs to make on iOS side to take the advantage of chunked transfer. To make it simple consider an example of uploading an audio stream (produced by audio unit) to the server in chunked mode.

 

Below code creates a dummy URL where audio-streamed data will be uploaded.

Code Fragment 1

Refer the below code segment.

Code Fragment 2

 

Note: Use the run loop of a thread, which is responsible for streaming. It is hazardous to access scheduled stream from a different thread which is not owning the run loop.

Code Fragment 3

The input stream should be assigned to URL request. Refer Code Fragment 3 to achieve it.

 

Now it’s a time to create connection object with above created URL request. The connection object should also get scheduled with run loop. The reason is now known to us..!! Refer Code Fragment 4 to achieve it.

Code Fragment 4

Create a class and put down all the above code fragments as member function(s). Give the name of a class to ChunkTransfer.

Below code represents the implementation of audio recorder delegate, which produces audio data.

Code Fragment 5

 

Refer Code Fragment 6.

Code Fragment 6

 

Refer Code Fragment 7.

Code Fragment 7

 

Refer code fragment 8.

Code Fragment 8

 

The data is transferred to the server into chunks as data become available rather waiting for entire data to be available. It helps a lot to reduce the round trip time to server. Same strategy can be used while downloading the response also. In spite of collecting full response in the memory, we can start processing chunked data. In most cases, client can start parsing on storing these partial chunked data in one thread when the other thread is downloading response from remote server.

 

After all it matters much, how fast your application is responding..!

Exit mobile version