Created
August 18, 2012 09:32
-
-
Save darkseed/3385633 to your computer and use it in GitHub Desktop.
Objective C GCD singleton
This file contains 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
+ (MyObject *)sharedInstance | |
{ | |
static MyObject *instance; | |
static dispatch_once_t onceToken; | |
if (instance==nil) { | |
dispatch_once(&onceToken, ^{ | |
instance = [[MyObject alloc] init]; | |
}); | |
} | |
return instance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
u can remove "if (instance==nil)", GCD launches block only once! no matter to check for nil.
this is valid GCD singleton code: