objective-cipadios5nsurlconnectionnsinvocationoperation

how to use asynchronous NSURLConnection for multiple urls


First, I think iPad has only single NIC (or what ever hardware) for handling connection. So requests are queued and serviced sequentially and there can't be 2 connections running at the same time. Is this guess true?

I have UITabBar controller, where each tab's view controller download & parses a different JSON feed URL. I have created a singleton class that downloads feeds from single URL, then parses it and calls a delegate function of one of the view controllers when work is done. I have decided to use asynchronous connection in order not to hang the main thread.

URLConnection4Target * _conn = [[URLConnection4Target alloc] initWithRequest:request delegate:self startImmediately:NO];

So tabs are all accessible from the same window (not navigated one after another), and they use different URLs for different feeds.

My problem is how to design the connection in the singleton class to serve that multiple view controllers sequentially (or concurrently if possible).

I tried to use NSInvocationOperation because i was not sure what will happen if the user clicked on a one tab and accesses the NSURLConnection object while it's already accessible by different view controller.


Solution

  • First off, the number of network connections is not tied to the number of NIC (Network Interface Cards) or ethernet ports or whatever. Secondly you mention you are using a singleton pattern for your URLConnection? Without seeing your singleton, I'm assuming this is the reason you only seem to be able to open a single connection at a time. You can create as many URLConnections as you need for your tabs, don't restrict them to a singleton. Think URLConnection/request, N x requests/tab and you should be fine.

    So when a tab is visible/active, create the URLRequest for the content, let it work asynchronously in the bg but make sure any UI updates are posted back to the main thread. Finally when the request completes, deallocate and forget about it.