-2

I came from another programming language. I can understand singleton pattern. But I got confusion in ObjectiveC's Singleton Implementation.

Actually, I understand the lifetime of a static variable. But What makes a static variable initialize only once?

@implementation MyManager

+(instancetype)sharedInstance {

    // structure used to test whether the block has completed or not
    //Doubt 1 - If this method called second time, how it is not reset again to 0. How it is not executed second time?
    static dispatch_once_t p = 0;

    // initialize sharedObject as nil (first call only)
    //Doubt 2 - If this method called second time, how it is not reset again to nil, How it is not executed second time?
    __strong static MyManager * _sharedObject = nil;

    // executes a block object once and only once for the lifetime of an application
    dispatch_once(&p, ^{
         _sharedObject = [[self alloc] init];
    });

    // returns the same object each time
    return _sharedObject;
}

@end
3
  • static variable only get initialized once. you can easily verify it. probably duplicated to some other question. and please clearly state your question. I bet most people reading it will miss the question hidden in someone else's comments.. Commented Feb 13, 2017 at 4:37
  • 2
    Your confusion seems to revolve around the static keyword. Objective C inherits from the C language, and it behaves exactly the same. stackoverflow.com/a/572550/3141234 Commented Feb 13, 2017 at 4:39
  • take a look here stackoverflow.com/a/16208466/3110026 Commented Feb 13, 2017 at 4:42

2 Answers 2

2

In computer programming, a static variable is a variable that has been allocated statically so that its lifetime or "extent" extends across the entire run of the program.

https://en.wikipedia.org/wiki/Static_variable

Sign up to request clarification or add additional context in comments.

3 Comments

Actually, I understand te lifetime of a static variable. But my doubt is "What makes a static variable initialize only once?". Got cleared at stackoverflow.com/questions/5567529/…
@particle Has your question been answered? If so, please accept one of the answers.
Thanks, I got the answer.
1

The call to dispatch_once makes it initialize only once.

dispatch_once takes a pointer to a static memory location. It does effectively the following:

lock location; if anyone else has locked location, block until we can
if (location is unset) {
   do_thing
   set location
}
unlock location

But it does this in a much faster way that doesn't require a real lock (it does require a special CPU instruction, though, called "compare and swap.") If you want more details, see Mike Ash's excellent explanation. But for most uses, you can just accept that dispatch_once, if used correctly, will only run once per execution of the program.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.