Reputation: 35953
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.
Upvotes: 5
Views: 170
Reputation: 17724
sharedMyManager
is a static variable, so by definition its initializer will only run once. The first time (only) the block is entered it is set to nil
. Then the dispatch_once
block runs and assigns it, then sharedMyManager
is returned.
For all subsequent calls, the initialization to nil
does not happen, nor does the dispatch_once()
content get reexecuted, so effectively all that happens is the return of the initialized variable.
Upvotes: 5
Reputation: 2445
dispatch_once() instantiates the class once.
So after calling sharedManager() once, the singleton is already there, and re-used.
Upvotes: 0