0

I understand that dispatch_once is something that is equivalent to a static variable and that the piece of code inside dispatch_once is executed only once throughout the application.

I am going through a huge code base and came across something like this

+ (DBHelper *)sharedInstance {

    static DBHelper *sharedDBHelper = nil;
    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{
        sharedDBHelper = [[super alloc] initUniqueInstance];
    });

    return sharedDBHelper;
}

DBHelper.sharedInstance is used to get an object and is used in a lot of places ot generate objects.

I'm confused as to why dispatch_once is being used here since that would mean you could have only one object of the class?

rowana
  • 718
  • 2
  • 8
  • 21
  • 2
    the `sharedDBHelper` is assigned only once in runtime – that is a standard pattern of creating _singleton_ instances. – holex Jun 29 '17 at 15:08
  • So what exactly happens when multiple classes use DBHelper.sharedInstance() and get an instance in return? Do they all refer to the same object? – rowana Jun 29 '17 at 15:11
  • 2
    that means that at first occasion the new instance is created and every further occasion the already existing instance will be used only – this is why it called singleton, because there is only one instance exists in the memory of the actual object during runtime. – holex Jun 29 '17 at 15:16
  • Great! Thank you so much! :) – rowana Jun 29 '17 at 15:16

2 Answers2

1

This is the standard pattern for a shared instance, otherwise known as a faux singleton.

In many cases programmers choose to use a single object that can be easily accessed from any part of an application - by calling a static method which returns back a reference to the shared object, i.e. sharedInstance in your example - as a means to provide communication/shared data between otherwise independent parts of the application.

It is a faux singleton pattern as it does not prevent other instances of the same type - DBHelper in your example - from being created. A true singleton model is one in which only a single instance of the type can ever be created. (Apple used to have sample code showing how to create true singletons, but it was never updated for the post-ARC world, for more details on that including an ARC version see this answer.)

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
0

It's a singleton (a design pattern). You only need one instance of the class instantiated, so that's all you create.

Cesar
  • 2,027
  • 2
  • 18
  • 29
  • So what exactly happens when multiple classes use DBHelper.sharedInstance() and get an instance in return? Do they all refer to the same object? – rowana Jun 29 '17 at 15:11
  • 2
    @rowana - Yes, they all refer to the same instance. And, BTW, the reason they use `dispatch_once` pattern is precisely in case multiple threads call this at the same time. This thread-safe pattern ensures if multiple threads call this at the same time, that the first will instantiate the object and all the rest will retrieve that same, single instance. – Rob Jun 29 '17 at 15:41
  • That helps! Thanks Rob! – rowana Jun 29 '17 at 15:59