Re: The current preferred method for fetching HTTP URLs for IOS
Re: The current preferred method for fetching HTTP URLs for IOS
- Subject: Re: The current preferred method for fetching HTTP URLs for IOS
- From: Andreas Grosam <email@hidden>
- Date: Sat, 10 Mar 2012 09:46:22 +0100
c
On Mar 9, 2012, at 8:14 PM, Alex Zavatone wrote:
> I've been reading (and trying out) a few approaches on HTTP communication using GCD to do a dispatch_sync or dispatch_async to a dispatch queue or using an NSURLRequest.
>
> Which of these is the preferred method for ingesting strings from HTTP URLs on iOS? Are there any plusses to one over the other?
This certainly depends how large your data is which you want to receive and how expensive it is (CPU wise) to process the data.
I would use NSURLConnection / NSURLRequest in an asynchronous manner. You handle each chunk of incoming data in the connection's delegate method:
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.dataProcessor handleDataChunk:data];
}
Usually, your "data chunk processing method" will process the data synchronously. For smaller data, this approach is sufficient.
If the data received by the connection is large, the handling of data may take considerably time, and if the delegates are scheduled on the main thread your main thread may become busy, and the UI actions may stutter. A simple approach is to save the data to a temporary file, then process it. However, on devices with faster hardware and more than one CPU, this approach is suboptimal performance wise.
Another naive approach to alleviate the above problem, would be to asynchronously schedule the method handleDataChunk: onto a dispatch queue. However, this approach is not scaleable for large data and may not work on restricted devices with limited memory and slow CPUs. While the main thread passes the work to another thread and remains responsive, all the received data chunks will be queued up in the dispatch queue waiting there to be processed. If the data comes in fast, and the processing is slow and if you have large data sets, well this may tie up too much memory on iOS and you crash.
If you have to implement a scaleable solution, which shall be able to handle large data and yielding optimal performance a more elaborated solution is required. This would essentially require to block the thread where the connection receives data which in turn will force the underlaying connection to stop reading further data from the socket.
You can active this as above, except that you need to design your handleDataChunk: such that it blocks if it received a data chunk and is currently processing the data. Then you need also to start the NSURLConnection on a secondary thread so that your delegate will be executed on this secondary thread.
Andreas
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden