Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is this code WORKING?

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.

like image 855
Duck Avatar asked Jan 14 '14 04:01

Duck


People also ask

Why does code NO WORK?

Here's a list of some of the reasons code doesn't work: Forgot to clear the browser cache: You may make CSS changes and think they don't work when they actually do because the browser is actually displaying an older version of pages which include the older code. Clear your browser cache to make sure.

How do codes work?

How does coding work? All code tells a machine to perform a specific task. Whenever you use the Internet, your device uses binary code –– a sequence of "1s" and "0s" that tells your computer what switches to turn on or off. This serves as a reliable way to store data and process information.


1 Answers

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.

like image 186
par Avatar answered Oct 18 '22 20:10

par