Skip to content

Handling multiple CPURLConnections with a common delegate

This week, Sheehan and I came across a problem that I’m sure others have too. The solution was obvious, once we figured it out.

The issue is: how can a single class that creates multiple asymchronouse CPURLConnections handle the responses.

Obviously, the class itself can only have a single delegate method:

-(void)connection:didReceiveData:

Our first thought was to create separate delegate classes just to handle those connections and set the delegate of the connection to an instance of that class. In fact, that turned out to be a good solution for requests that were made from different controllers.

But we still had a case, where we wanted to have the connection handled in the same class because, upon receipt of the response, certain actions had to take place. The solution we came up with turned out to be quite simple. We assigned “private” variables to those connections and in the delegate method, checked if the connection was the equal to the private variable.

var request = [[CPURLRequest alloc] initWithURL:[CPURL URLWithString:@"http://api.myserver.com]];

_firstConnection= [CPURLConnection connectionWithRequest:request delegate:self];

request = [[CPURLRequest alloc] initWithURL:[CPURL URLWithString:@"http://api.myserver.com/posts]];

_secondConnection = [CPURLConnection connectionWithRequest:request delegate:self];

-(void)connection:(CPURLConnection)connection didReceiveData:(CPData)data
{
    if (connection == _firstConnection) {
        [something doThis];
    }
    if (connection == _secondConnection) {
        [something doThat];
    }
}

– Dan

Advertisements

Need a place to store random code you find on the web…CodeBox

A couple of days ago i bought my first App in the Mac App Store. CodeBox. CodeBox is a very simple program that lets you paste into it code that you find on the web. It also has syntax color coding and a simple, yet effective organization method.

Additionally, the author of the program is very responsive to Feedback email. he often responded to me within hours, if not minutes of my request.

I would encourage all coders to buy it today and support the developer. Here is the link to CodeBox on the Mac App Store.

Cappuccino nib2cib bindings are here.

I probably love programming Cappuccino apps as much as I love programming in Cocoa. Until last week though, one of the most powerful features in Cocoa, bindings in the NIB file, was not available. To bind NIB object values to a key path, you had to do so programmatically:

[myTextField bind:@”value” toObject:anObject  withKeyPath@”aKey” options:nil];

This also meant I had to create outlets for each control I had to bind and then connect those controls in the NIB.

Now, I thrown in an instance of CPObjectController (or one of it’s subclasses), set the content, and set the bindings to all the controls right in the NIB. Far less work.

280North, the folks who created Objective-J and Cappuccino, was bought by Motorola last year. Happily, development of this awesome frame hasn’t slowed a bit and perhaps has even sped up. More and more apps are appearing on the web, and I imagine there are many more to come. Of the new “full application” frameworks that have recently emerged, it certainly seems to be taking an early lead.

Tower rocks my GIT.

I will be the first to admit, I am not a command-line junkie. I like using GUI as much as I like programming apps that use a GUI. No doubt, GIT is the current preferred version control system out now and it’s great. At first, I used command-line, next came the GIT-plugin for TextMate.

I recently started a new gig helping StockTwits, a San Diego company, develop a new app using Cappuccino. One of the regular employees there introduced me to Tower. After a month of using it, I can’t imagine using anything else. It’s in beta now and FREE! If the developers release the final version as a paid app, they can count on at least one sale!

Hello world!

Welcome to my new blog. I will use this blog to share tips, hints, tricks, and general observations regarding programming Cocoa, iOS, Cappuccino & GNUstep along with Objective-C and Objective-J. If I could just help one person, then it will all be worth it.

Thanks

Dan