+ (XXXXStore *)sharedInstance
{
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
按理说static已经保证了是类级别唯一,那么为什么还要用dispatch_once呢?
原因是为了线程安全:
dispatch_once不仅意味着代码仅会被运行一次,而且还是线程安全的,这就意味着你不需要使用诸如@synchronized之类的来防止使用多个线程或者队列时不同步的问题。
可以参考:
http://www.cnblogs.com/hellocby/archive/2012/08/24/2654488.html