Looking for objective-c Keywords? Try Ask4Keywords

Objective-C Language Usando Grand Central Dispatch (GCD)


GCD garantizará que su singleton solo se ejemplifique una vez, incluso si se llama desde múltiples hilos. Inserta esto en cualquier clase para una instancia de singleton llamada 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;