Tracking multiple NSURLConnections

A couple of years back, when I first started playing with the new Leopard (10.5) ABI’s, I was looking for a project that would allow me to play around with things like Assocaiated References (AR’s) and Blocks

I spend most of my free time maintaining both Weather Vane and Audio Switcher, which still run under 10.4 and so can’t make use of any of the new ABI. After looking at a few of the examples of the new stuff on the ‘net, I decided I would try to use them to track multiple NSURLConnections. If you need to create and dispatch multiple, simultaneous¬†NSURLConnections, you’ll find that the ¬†NSURLConnection ABI does not provided a means of distinguishing between the data returned from each connection. One obvious and simple solution to this problem is to subclass NSURLConnection and add some identifying field that distinguishes each connection. I did just this some years back and thought at the time, that it seemed silly to have to do that. So, I thought I’d redo that project specifically using AR’s to track each connection. If you read the docs, you know that you can use AR’s to associate arbitrary data to a given object. When the connection returns data, you inspect the data you associated with it to determine which connection it is and then do whatever you need.

This actually works pretty well, but I wanted to get some opinions from other Cocoa developers about this method of tracking connections. You can read the thread here. Many of the comments were enlightening and gave me a couple more ideas to try. Well, I’ve finally found the time to re-work the project, but this time, instead of trying to shoehorn some ABI into the project, I tried to accomplish this task in a smarter way.

So, this time I decided to wrap an NSURLConnection in an object that acts as the delegate for its connection. This proved to be much cleaner, as now the connection object essentially tracks itself. Of course, this assumes that each connection needs to handle the returned data in the same way. If different connections need to behave differently, you’ll need to try something else. The project creates multiple http request and tracks the time it takes for the headers to come back and then how long it takes for the body of the request to completely load. Not exactly sure if how I time the connections is correct, but it’s neither here nor there for this project. The important part is the connection tracking. Here’s a screen shot:

NSURLConnection timer

Each object is added to an NSArrayController and its fields are mapped to the NSTableView’s columns. Unlike the first project, though, there is no logic in code to determine which connection is which. As the values change in each connection, Cocoa Bindings handles their display, which keeps things nice and clean. You download the project here. There is a file “entries” included with the project. Its just a plist of http urls you can use to populate the table instead of manually typing in urls, if you prefer. I haven’t done a line by line comparison of the two projects, but there is at least 30% reduction in code. I’ve decided not to upload the original project. It ¬†could use a bit of clean up, but if anyone wants to look at it, I’ll go ahead and post it.

As always, if you have ideas or questions, feel free to post.