Proper thread-safe singleton
February 26, 2013 in iOS Snippets
The following is a correct thread-safe implementation of the singleton +sharedInstance method, which relies on GCD:
+ (id)sharedInstance;
{
static id sharedInstance = nil;
static dispatch_once_t once = 0;
dispatch_once(&once,
^{ sharedInstance = [[self alloc] init]; }
);
return sharedInstance;
}