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?