Asynchronous Loading of Images in a UITableView

This is one of the most common scenarios for network- + UITableView-based applications; a UITableView instance whose contents come from the network; not only the text, but also the images! Somehow we all want to reproduce the behavior of the App Store (or of the iTunes) iPhone application, where the icons (or covers) of the apps (or songs) are downloaded one by one, as you scroll the table, without crashing nor opening 1000 simultaneous network connections.

My solution (there might be many others!) consists in the following key elements:

  • Avoid loading images right after the RSS feed is parsed; instead, use the -tableView:willDisplayCell:forRowAtIndexPath: delegate method in the controller to trigger the loading of the image for cells that become visible; this ensures that only the cells that are visible will receive the order to load images from the network;
  • Use a “model” class for each element of the table, and make the custom UITableViewCell subclass a delegate of this model object; then, the model object is responsible of loading its own image, and will tell the UITableViewCell when done;
  • Use the ASIHTTPRequest framework, with a shared download queue in the application delegate, so that all of the requests are properly queued, and network resources are properly used;
  • Show feedback to the user with scrolling wheels whenever and wherever appropriate;
  • Use the Reachability class from Apple’s own sample code to see if we’re really connected to Flickr, and otherwise show an error to the user.

I have created a sample project, as usual in Github, where I gather RSS data from Flickr’s public feed, and then I download synchronously the preview images of the feed. I even included a very simple Core Animation effect which reminds me of how the iTunes iPhone application allows us to hear previews of the songs we want to buy (the image flips and shows another view “behind”).

Feel free to contribute, fork, enjoy, read, use in your own projects, as you want. As I said, there might be many other (and most probably better) approaches to do this, so feel free to leave your comments below, as usual.

The Dirty Little Secret of iPhone Development

This is happening right now, at a web agency near you.

The dot-com boom of the 90′s spawned a brand new generation of coders and software developers, including me, by the way. While before that time the term of “software developer” might have been reserved to system programmers fluent in C, COBOL, C++ or other languages, right now the vast majority of developers I know spend their time writing web applications, either public or in a private intranet, in J2EE, ASP.NET, Rails, PHP, you name it.

I have said before that writing web applications should be taken as seriously as writing desktop systems. Call me names if you want, but I’m a big fan of Joel’s Test.

However, after all this years, after the Chaos reports, after Peopleware, after the Mythical Man Month, people still treat quality as an afterthought. And also complain about how much software sucks, how expensive it is, and how late it arrives, by the way. Now that the iPhone SDK is widely available, that the App Store is selling more apps that we could have had imagined 6 months ago, many web agencies want to jump to native iPhone development contracts, which are hype and nice and pricey and whatnot. Which is only going to make things worse.

The dirty little secret in this story is this: iPhone development looks more like developing applications for a desktop operating system, and less, much less than web development. And I’m frightened to see some small shops (and even bigger ones), who never attained a real level of professionalism or quality in their software tasks, starting projects and realizing, later, when they are over budget and behind schedule, that this kind of applications requires a different mindset.

Continue reading

A Watch – from an OOP perspective

A watch might be one of the most common types of objects, but it remains also one of the earliest pieces of human craftmanship to show an extreme level of complexity, all contained in a small amount of space. Since the late 1700s, artisan watchmakers in Switzerland and elsewhere have shown their pride and skills creating watches called “Grande Complications”, containing thousands of individual parts and performing incredible functions:

The most complicated watch ever made, known in watch enthusiasts’ circles as “The Ultimate Watch,” is Patek Philippe’s “Calibre 89. The incredibly precise operation of 1728 parts in this really ultimate masterpiece of watchmaking allows to perform no less than 33 (thirty-three!) complicated functions, among them a correction for the 400-year-rule, an Easter date indication, a star chart, a tourbillon, a perpetual calendar, a sidereal time indication, and, and, and … This watch was sold in 1989 for the nice round sum of about four million Swiss francs.”

(Ozdoba, 2005)

(Source: CNN.com, 2005)

More information about the “Calibre 89″ can be found here and in the Patek Philippe Museum website.

However, the same watchmakers that made these fine pieces were also aware of the basic information that their creations were to provide: time. As such, their watches remained extremely easy to use, and they set up the basic standard for analog watches, in such timeless designs that the latest Swatch models show the same basic layout and functionality.

The underlying concept is the very same used in today’s object-oriented abstraction and encapsulation. Even Apple uses the idea of the watch to show this characteristic:

All programming languages provide devices that help express abstractions. In essence, these devices are ways of grouping implementation details, hiding them, and giving them, at least to some extent, a common interface—much as a mechanical object separates its interface from its implementation, as illustrated in Figure 2-1.

(Source: Apple Developer Connection, 2006)

In this article I will provide my view about how different OOP concepts apply to a real-life object such as a watch, in all its forms. Continue reading

The beauty of Cocoa

(Highly geeky post ahead. You’ve been warned!)

I have found the very message that summarizes the beauty of Cocoa in a single word; see by yourselves, hereunder in line 47:

[source:c]

import

// The interface of a person @interface Person : NSObject { NSString* firstName; NSString* lastName; int age; } @end

// The implementation of the Person @implementation Person -(id)init { if (self = [super init]) { firstName = @”"; lastName = @”"; age = 0; } return self; }

-(void)dealloc { [firstName release]; [lastName release]; [super dealloc]; }

-(NSString*)description { return [[NSString alloc] initWithFormat:@”Name: %@ %@, %d years old”, firstName, lastName, age]; } @end

// Some code using the Person class int main (int argc, const char * argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSMutableDictionary* dict = [[[NSMutableDictionary alloc] init] autorelease];
[dict setObject:@"Teto" forKey:@"firstName"];
[dict setObject:@"Rodriguez" forKey:@"lastName"];
[dict setObject:[[NSNumber alloc] initWithInt:34] forKey:@"age"];
Person* person = [[[Person alloc] init] autorelease];
// The beauty of Cocoa can be resumed to this very line:
[person setValuesForKeysWithDictionary:dict];
// Now sit back and relax:
NSLog([person description]);
[pool drain];
return 0;

} [/source]

Pastrami Sandwich

I find many similarities between an event like WWDC and a similar one I’ve attended at Redmond long ago; both are big (huge!) events, with thousands of (men) engineers from around the world (and very few women), with a keynote by the founder, lots of events every morning and afternoon, and merchandasing stuff all over the way. And of course, in both cases you get food boxes for lunch.

However, there is one basic difference between both events. Apple not only has interesting technologies to show up, even bleeding edge ones, more often than not on the open and public domain (many of which I can not write about, and boy they are going to make a difference!), but even better than that, it has a vision.

And passion. Cocoa developers are among the most passionate I’ve ever met, and you just can’t find that in a Microsoft event. You can feel that in the (conditioned) air of the Moscone center, almost touch it. New projects everywhere. People discussing about their ideas. Lots of collaboration, openness and willingness to go further. Microsoft’s stuff is, well, boring at best; dull and gray. Enterprise IT is no fun, believe me, but there’s no reason to try to look at it in a different way. And faithful to its own way, Apple is precisely doing that, right now; and what’s about to come will reshape the industry forever. Continue reading