-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
partikles
  • 331
  • 3
  • 13
  • 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.. – Bryan Chen Feb 13 '17 at 04:37
  • 2
    Your confusion seems to revolve around the `static` keyword. Objective C inherits from the C language, and it behaves exactly the same. http://stackoverflow.com/a/572550/3141234 – Alexander Feb 13 '17 at 04:39
  • take a look here http://stackoverflow.com/a/16208466/3110026 – Mrugesh Tank Feb 13 '17 at 04:42

2 Answers2

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

Alexander
  • 59,041
  • 12
  • 98
  • 151
  • Actually, I understand te lifetime of a static variable. But my doubt is "What makes a static variable initialize only once?". Got cleared at http://stackoverflow.com/questions/5567529/what-makes-a-static-variable-initialize-only-once – partikles Feb 13 '17 at 05:21
  • @particle Has your question been answered? If so, please accept one of the answers. – Alexander Feb 13 '17 at 05:43
  • Thanks, I got the answer. – partikles Feb 13 '17 at 05:50
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.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610