I am working on an application that needs to share data in a plist between several different controllers. Apple has an example of what I want to do, but I don't understand the code they used. I did some digging yesterday and this morning, but I don't understand the dispatch_once_t pred & dispatch_once(&pred, ... stuff. My best understanding is that these lines stop the compiler from making more than one instance of the class, but I am not sure. Any help would be great, even if it is a document to read. Thanks.
Apples' Code:
// we use the singleton approach, one collection for the entire application
static PeriodicElements *sharedPeriodicElementsInstance = nil;
+ (PeriodicElements *)sharedPeriodicElements
{
@synchronized(self) {
static dispatch_once_t pred;
dispatch_once(&pred, ^{ sharedPeriodicElementsInstance = [[self alloc] init]; });
}
return sharedPeriodicElementsInstance;
}
Apples' Code:
// we use the singleton approach, one collection for the entire application
static PeriodicElements *sharedPeriodicElementsInstance = nil;
+ (PeriodicElements *)sharedPeriodicElements
{
@synchronized(self) {
static dispatch_once_t pred;
dispatch_once(&pred, ^{ sharedPeriodicElementsInstance = [[self alloc] init]; });
}
return sharedPeriodicElementsInstance;
}