Categories
Development

Hello, rdd.me

Readability Blog: “One of the more useful tools in the Readability toolbox has been our link shortener, rdd.me. The interesting bit about rdd.me is simple: When an article is linked to through rdd.me, it offers up the availability to view the content in a clean, readable view.”

Get to building! If you need some Objective-C to talk to rdd.me, you’re in the right place.

Take a look at RFRddMe.

Categories
Development

Asynchronous Unit Tests

AHHHHHH!When I created the project for RFRddMe I added Unit Tests. Every developer should do this today. They’re easy to create and Xcode does a great job getting you started. Everything is setup, you just need to write and run the tests. Really great stuff!

The only challenge for me was how to test asynchronous code? As far as I know it’s up to the developer to figure that out. That’s fine, I have a solution, but I thought I’d share it and see if anyone has an opinion about how I did it.

If you haven’t used the Unit Test Framework included with Xcode, you should. It’s a cut of SenTestingKit, and it does the trick.

It provides a couple of methods that allow you to setup and tear down a unit of tests and all you need to do is write test methods. Here’s an example test method, that does nothing.

- (void)test;
{
    STAssertTrue(true, @"test");
}

So, how in the world do you test asynchronous code? The way you write any other asynchronous code. You need a way to block until the code you’ve executed is finished.

The RFRddMe projects makes use of ASIHttpRequest, which runs in a thread. This project requires the implementation of an protocol, RFRddMeProtocol, which provides a mechanism for your application to receive results. This is all really cool because your application’s UI will remain responsive while you wait for results, but I digress.

The bottom line is we have to block while we wait on our results to return. So, here’s what I did to make this work. I created a method, I dubbed hackyRunLoopWait, that makes use of the default NSRunLoop to wait for completion of our asynchronous methods to complete. Here’s what it looks like.

// Introduced methods.
- (BOOL)hackyRunLoopWait;
{
    // Reset our _done flag.
    _done = NO;
    
    // Reset the _succeeded flag to NO.
    _succeeded = NO;
    
    // Get the current run loop.
    NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
    
    // Spin until the _done is YES or we've waited for 90 seconds.
    // Yep, it's a complete hack, just to get this working.
    while ((NO == _done) && [runLoop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:90]]);
    
    // Need to deal with exiting the loop above because we timed out.
    return _succeeded;
}

Here’s how I used it to test methods on RFRddMe.

- (void)testShorten;
{
    RFRddMe* p = [[RFRddMe alloc] initWithDelegate:self];
    [p shortenUrl:@"http://iam.fahrni.me/2011/11/15/for-posterity/"];
    
    STAssertTrue([self hackyRunLoopWait], @"testShorten");
    [p release];    
}

Notice the STAssertTrue() statement, it contains [self hackyRunLoopWait], which blocks and waits on our shortenUrl method to complete.

Another thing to make note of is RFRddMeProtocol is implemented by the test class, see the [[RFRddMe alloc] initWithDeelgate:self]. When the request completes it will notify the shortenSucceeded method, which sets the values monitored in hackyRunLoopWait, which allows that method to return.

// RFRddMeProtocol Implementation
- (void)shortenSucceeded:(RFRddMeShortUrl*)shortUrl;
{
    _succeeded  = YES;
    _done       = YES;
}

Is this the best way? I don’t know, but it does what I need it to do, and it works as expected. If someone has a better idea I’d love to hear from you. Please, leave a comment, or send an email; rob.fahrni@gmail.com.

Categories
Development iOS Mac

Fresh Code: RFRddMe

Earlier this week Dave Winer pointed out some neat stuff Readability was up to. Part of the piece pointed out a new URL shortener. I marked it and came back to it today. Since I love writing code to talk to RESTful web services, why not write another one?

The Red Readability CouchThis afternoon I started on RFRddMe, an Objective-C library for the Readability Shortener Service. Late this afternoon I completed the library, and I checked it into my GitHub Repository tonight. Figuring out git submodules took a bit of time, but it works as advertised.

If you just happen to be looking for Objective-C code to shorten a URL, and add an article to Readability, look no further.

Get the code for RFRddMe on GitHub.

Please, drop me a line, rob.fahrni@gmail.com, if you use the code.

Enjoy.

Categories
Development Indie Life

For Posterity

I started down the Mac Development road last night.

By this time next year...

Categories
Development

Fragile, must be Italian?

Nick Bradbury: “I wrote the first version of HomeSite back in 1994, and seventeen years later I can still run it on the latest version of Windows.

I created FeedDemon 1.0 in 2003, and it was the first app I wrote that relied on web APIs. Now those APIs no longer exist, and almost every version of FeedDemon since then has required massive changes due to the shifting sands of the web APIs I’ve relied on.”

Categories
Development Mac

Don’t Panic!

CodaPanic: “Coda 2 has now been in development for about a year and a half. All of us have been working incredibly hard on this forthcoming release. We’re finishing up new features, boosting up the editor, dramatically cleaning up the UI, and improving what Coda already does well today, all while, hopefully, keeping things extremely light and lean. By the time you see it, Coda might look a little different than you’re used to, but we think it’s for good reason. We’ll see how it shakes out, but we’re very excited.”

I’m sure the Coda update will be a work of art, Panic doesn’t know how to create bad software.

Categories
Business Development

One Developers Journey

Johnny AppleseedKevin Hoctor: “The irony is that I’m the one always preaching to others that they need to be passionate about what they do. Don’t settle for a paycheck. Don’t work at a place that makes you want to rush home and down a bottle of vodka to balance out the day. Yet, here I am doing exactly what I’ve told my kids not to do.”

Kevin made the transition from frustrated Windows developer to Indie Mac developer and is still going strong. About six months after the above post Kevin shipped Debt Quencher 1.0. I don’t think he’s looked back.

That is an inspiration.

Categories
Development

Objective-C REST and JSON

Bringing in the HarvestThe first REST based code I wrote for the Mac and iOS was my P8 Library for ping.fm. That code is all NSRequest based and I learned a lot while doing it. Since that time I’ve done a couple more projects that consumed REST Services and XML or JASON.

To that end I thought I’d record some of the libraries I’ve run across. Some I’ve used, others are a curiosity.

REST Libraries

ASIHTTP – I’ve used this library a couple of times and I’m using it on a new project. I like it, but the developer that lovingly created it is giving up on it because people gave him too much crap. That’s a real shame, not only that he’s done with it, but that people would harass him so much he gave up on a very handy piece of code.

LRResty – A simple REST/HTTP client. Apparently this library was inspired by a Ruby implementation. I’m fairly certain this is the library I’ll be moving to this in the future.

RestKit – This library not only deals with retrieving data, it will also map it into Core Data. Pretty cool, and if you have need to cache data locally it would be worth looking at.

JSON

SBJson – A very nice JSON parser. It’s simple to use and includes handy categories for NSString and NSDictionary. It’s very simple to create a parser and operate on the results, just like you would an NSDictionary.

SBJsonParser* jsonParser = [[SBJsonParser new] autorelease];
id jsonObject = [jsonParser objectWithString:jsonString];

JSONKit – This is a parser I only recently discovered, and it’s known to be fast and efficient. It’s also super easy to use because of, you guessed it, categories. There’s a real nice one added to NSString.

NSDictionary* response = 
[[request responseString] objectFromJSONString];

See how easy that is to use? You simply invoke objectFromJSONString to get a fully parsed result jammed into an NSDictionary.

Categories
Apple Development Objective-C

ARC

ARCMike Ash: “That, in essence, is what ARC is. The memory management rules are baked into the compiler, but instead of using them to help the programmer find mistakes, it simply inserts the necessary calls on its own.”

Reference counting is a pain in the keister, until you understand the rules. ARC is going to become an important tool in the quest to creating error free code. If you’ve ever written ref counted code you know how careful you need to be so you don’t leak memory or double-release something and cause a crash. It can be frustrating if you don’t pay attention.

This is a welcome change and one I’m looking forward to.

I think it’s time for an RxCalc refresh anyway. Plus some other nifty things I’ve been working on, that will definitely be ARC’d and Mac OS X Lion / iOS 5 only.

Categories
Development Technology

Just a little dramatic

A wonderful boquet of flowers.Joe Hewitt: “The Web will be just another app that you use when you want to find some information, like Wikipedia, but it will no longer be your primary window. The Web will no longer be the place for social networks, games, forums, photo sharing, music players, video players, word processors, calendaring, or anything interactive. Newspapers and blogs will be replaced by Facebook and Twitter and you will access them only through native apps. HTTP will live on as the data backbone used by native applications, but it will no longer serve those applications through HTML. Freedom of information may be restricted to whatever our information overlords see fit to feature on their App Market Stores.”

I’m mostly ok with most of what Mr. Hewitt has to say in this post. Native clients are still provide the best shot at a great User Experience, in my humble opinion. The web in all its glory is just a network, why not use it? I don’t care if the browser is my default window, why would I? It provides a lowest common denominator experience, not the best experience.

It sounds like he wants a web browser with the power of a desktop application. Why not just write desktop applications? Is it because he wants a common host? Maybe, maybe not?

It also seems like Mr. Hewitt is a glass half empty sort of fella. Most of his recent posts seem, at least to me, to be all gloom and doom. It’s not that bad, really, it’s not.