Looking for objective-c Keywords? Try Ask4Keywords

Objective-C Language Uso di Grand Central Dispatch (GCD)


GCD garantisce che il tuo singleton viene istanziato una volta sola, anche se chiamato da più thread. Inseriscilo in qualsiasi classe per un'istanza singleton denominata shared .

+ (instancetype)shared {

    // Variable that will point to the singleton instance. The `static`
    // modifier makes it behave like a global variable: the value assigned
    // to it will "survive" the method call.
    static id _shared;

    static dispatch_once_t _onceToken;
    dispatch_once(&_onceToken, ^{

        // This block is only executed once, in a thread-safe way.
        // Create the instance and assign it to the static variable.
        _shared = [self new];

    return _shared;