Hello World!

It's a brand new year for all of us—we all have goals we'd like to accomplish, new heights to reach, new stories to tell and past experiences to forget. Some may take on the feat of losing weight, changing bad habits, or switching career paths; for me, this year is different.

The year 2014 will go down as an incredible and possibly the most exciting year in my life thus far! Last year finished off with a shift from full-time designer to full-time developer. With just two weeks into 2014, I can honestly say that I'm beginning to grok these 3000+ iOS/Cocoa APIs—it took a year, but I am very determined to grow as fast as I can.

Now that I am starting to grasp the foundation behind iOS development, the Objective-C language, and learning the fundamentals of git, I can now focus my time on creating great human experiences via iPhone apps.

I think this blog will be a random collection of web designs, app designs, development tips (web & mobile), source code from various projects, and some personal observations in the process. Hopefully, I can look back at this post next year and be thankful of how far we've come. Keep calm and happy coding...

Keep Calm and Happy Coding

Objective-C Singleton Patterns (with ARC)

Sometime last year I learned how important singletons can be in the iOS sandbox environment. These objects can be globally instantiated and manipulated without issues (most of the time). After realizing that the majority of my model classes can be supported via singletons, I decided to do some research into how Apple utilizes their low-level blocks for singleton creation.

If you've developed for and iPhone or iPad app that makes any RESTful calls, you've definitely seen a singleton before, as they are fairly easy to create (and also easy to abuse!)—for those of you who haven't, here is a basic example:

+ (InstanceType *)sharedInstance {
    static InstanceType *sharedInstance = nil;
    if (sharedInstance == nil) {
        sharedInstance = [[InstanceType alloc] init];
    }
    return sharedInstance;
}

Now, there are a few issues with this implementation that you should know about. Firstly, this class method is NOT thread safe, so if multiple threads attempt to access this object at the same time you will present a race condition.

Although Apple has shown this example in previous documentation (iOS 4.0+), you should not utilize this pattern today.

If you would like to use singletons, use dispatch_once():

dispatch_once() solves this threading issue because: (1) it guarantees the block will only be called once for the lifetime of the application, (2) it is thread safe, and (3), due to its low-level concurrency it is faster than other methods such as @synchronous(), etc.

"If called simultaneously from multiple threads, this function waits synchronously until the block has completed."

It's also semantically cleaner, because the entire idea of dispatch_once() is "perform something once and only once".

Xcode Screenshot

Many of the classes you've already utilized in your projects are built with the dispatch_once() singleton pattern:

[NSUserDefaults standardUserDefaults]
[UIApplication sharedApplication]
[UIScreen mainScreen]
[NSFileManager defaultManager]

Here is an example of a singleton pattern with a dispatch_once() block:

+ (InstanceType *)sharedInstance {
    static InstanceType *sharedInstance = nil;
    static dispatch_once_t onceToken; // onceToken = 0
    dispatch_once(&onceToken, ^{
        sharedInstance = [[InstanceType alloc] init];
    });
     return sharedInstance;
}

This is the safest (and most effective) way to create a singleton. There is no possible way to create two instances, and it is 100% synchronous and thread safe.

Comments