Created
March 2, 2012 20:46
-
-
Save gcox/1961202 to your computer and use it in GitHub Desktop.
Thread-safe singleton pattern using dispatch_once
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface Something : NSObject | |
+(Something *)somethingSingleton; | |
@end | |
@implementation Something | |
+(Something *)somethingSingleton { | |
static Something *something = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
something = [[self alloc] init]; | |
}); | |
return something; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More information: http://objcolumnist.com/2011/07/06/creating-singletons-using-dispatch_once/