Matt Galloway suggests this as the correct way to initialize a singleton:
+ (id)sharedManager {
static MyManager *sharedMyManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedMyManager = [[self alloc] init];
});
return sharedMyManager;
}
I have tested and it works correctly but I don't understand why. When the singleton is first created the variable sharedMyManager
is set with the contents of the init line but when the singleton is already created and I access it using [MySingleton sharedManager];
the first line of the code runs, setting sharedMyManager
to nil
and then the last line runs returning what is, in theory, nil
. In fact, it is returning the correct singleton, but why?
How can sharedMyManager
return the correct object if it is being set as nil
?
Attention because I am talking to the subsequent calls to sharedManager
, after the singleton was created.
I suppose the static keyword is doing the magic by now allowing the value to be assigned more then once, but if this is true, the init part should not work, because the static variable is being assigned to nil in the first place.
Please explain me as I was five years old. Thanks.