views:

62

answers:

2

I have a few apps that I am trying to develop a reusable URL connection layer. I have done some research and am struggling between architectures. Specifically the APIs this layer utilizes.

In the past, I have used NSURLConnection and NSOperation on a separate RunLoop. This seems overkill. I've seen libraries that subclass NSURLConnection. Others have a singleton Engine object that manages all requests.

The Engine and/or NSURLConnection seem best to me. But I am asking for input before I go too far down one road. My goals would be:

  • Ability to cancel a request
  • Concurrent requests
  • Non-blocking
  • Data object of current open requests

Any direction or existing references with code samples would be greatly appreciated.

+1  A: 

I'm not sure about a "data object of current open requests", but ASIHTTPRequest does the first three and is very easy to use.


Update

Actually, it looks like ASINetworkQueue may fulfill your last bullet point.

Robot K
+1 for the great reference. One I will use in the future for larger projects. However, I think the learning opportunity for building my own `NSURLConnection` subclass and *ConnectionEngine* class will be valuable for this small app.
Jason McCreary
A: 

I personally use a singleton engine with my large Apps though it might not always be the best case. All the URL's I use require signing in first, figured it would be best if one Class handles all of the requests to prevent multiple URLS from signing into the one location.

I basically create a protocol for all my different connection classes into my singleton and pass the delegate of both the calling class and the singleton into it. If an error occurs its passed to the singleton so it can deal with it, if it completes it returns the data to the calling class.

Rudiger
This seems to be the same approach as some of the Twitter Libraries I've seen such as MGTwitterEngine. This is currently how I'm setting up my architecture. So you're having the *Engine* handle manage the `NSURLConnection` and the calling class a delegate of the *Engine*?
Jason McCreary
Mmm, might of been lost in translation but I'm passing the calling class as the delegate to the Engine and then Engine passes itself and the calling class delegate to the class that handles the NSURLConnection. The class that handles the NSURLConnection then responds to either the calling class or the engine depending on whats happening. It might be a little complex as I said but I sorta needed to do it this way as the sign on process is an ordeal that can have a few errors.
Rudiger
Yes, we're on the same page. I actually have an authentication prepare step too. So I think for now, I'm going to go with creating my own as you suggested.
Jason McCreary