So I have a singleton and Im trying to understand the difference between these two implementations: functionally I have tried running my code with both of them and they both work
However, I notice that in the 1st implementation there is no [self alloc] being called instead the call is to [super alloc]. Im a bit perplexed by this. It seems to work but it seems a bit magical so Im wondering if someone can clarify
1st way:
+(id)getSingleton
{
static dispatch_once_t pred;
dispatch_once(&pred, ^{
locMgrSingleton = [[super alloc] init];
});
return locMgrSingleton;
}
Another way
+(id)getSingleton
{
@synchronized(self)
{
if (locMgrSingleton == nil)
{
locMgrSingleton = [[self alloc]init];
NSLog(@"Created a new locMgrSingleton");
}
else
{
NSLog(@"locMgrSingleton exists");
}
}
return locMgrSingleton;
}