Performing a Task at Most Once

Allocating and initializing a singleton is one of the tasks that has to happen exactly once during the lifetime of an app. I am sure you know of other scenarios where you had to make sure a piece of code was executed only once during the lifetime of your application.

GCD lets you specify an identifier for a piece of code when you attempt to execute it. If GCD detects that this identifier has been passed to the framework before, it won’t execute that block of code again. The function that allows you to do this is dispatch_once, which accepts two parameters:

Token

A token of type dispatch_once_t that holds the token generated by GCD when the block of code is executed for the first time. If you want a piece of code to be executed at most once, you must specify the same token to this method whenever it is invoked in the app. We will see an example of this soon.

Block object

The block object to get executed at most once. This block object returns no values and accepts no parameters.

Note

dispatch_once always executes its task on the current queue being used by the code that issues the call, be it a serial queue, a concurrent queue, or the main queue.

Here is an example:

static dispatch_once_t onceToken;

void (^executedOnlyOnce)(void) = ^{

  static NSUInteger numberOfEntries = 0;
  numberOfEntries++;
  NSLog(@"Executed %lu time(s)", (unsigned long)numberOfEntries);

};

- (BOOL)              application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

  dispatch_queue_t concurrentQueue =
    dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

  dispatch_once(&onceToken, ^{
    dispatch_async(concurrentQueue,
                   executedOnlyOnce);
  });

  dispatch_once(&onceToken, ^{
    dispatch_async(concurrentQueue,
                   executedOnlyOnce);
  });

  // Override point for customization after application launch.
  [self.window makeKeyAndVisible];
  return YES;
}

As you can see, although we are attempting to invoke the executedOnlyOnce block object twice, using the dispatch_once function, in reality GCD is only executing this block object once, since the identifier passed to the dispatch_once function is the same both times.

Apple, in its Cocoa Fundamentals Guide, shows programmers how to create a singleton. However, we can change this model to make use of GCD and the dispatch_once function in order to initialize a shared instance of an object, like so:

#import "MySingleton.h"

@implementation MySingleton

static MySingleton *sharedMySingleton = NULL;

+ (MySingleton *) sharedInstance{

  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    if (sharedMySingleton == NULL){
      sharedMySingleton = [[super allocWithZone:NULL] init];
    }
  });

  return sharedMySingleton;
}

+ (id) allocWithZone:(NSZone *)paramZone{
  return [[self sharedInstance] retain];

}

- (id) copyWithZone:(NSZone *)paramZone{
  return self;
}

- (void) release{
  /* Do nothing */
}

- (id) autorelease{
  return self;
}

- (NSUInteger) retainCount{
  return NSUIntegerMax;
}

- (id) retain{
  return self;
}

@end