Skip to content

Instantly share code, notes, and snippets.

@lukeredpath
Created June 30, 2011 22:18
Show Gist options
  • Save lukeredpath/1057420 to your computer and use it in GitHub Desktop.
Save lukeredpath/1057420 to your computer and use it in GitHub Desktop.
Macro for creating your "shared instance" using GCD
@implementation MySharedThing
+ (id)sharedInstance
{
DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
return [[self alloc] init];
});
}
@end
#define DEFINE_SHARED_INSTANCE_USING_BLOCK(block) \
static dispatch_once_t pred = 0; \
__strong static id _sharedObject = nil; \
dispatch_once(&pred, ^{ \
_sharedObject = block(); \
}); \
return _sharedObject; \
@lynnmatrix
Copy link

What if the sharedInstance is destroyed anywhere? Can it be created again?

@lukeredpath
Copy link
Author

No, it acts like a traditional singleton in that respect - only one can ever be created.

@tmr111116
Copy link

This code is error in Xcode 4.6

GCDSingleton.h:7:25: note: expanded from macro 'DEFINE_SHARED_INSTANCE_USING_BLOCK'
  return _sharedObject; \

Last \ is not need.

@skozin
Copy link

skozin commented Jan 8, 2014

Thank you for sharing such a great technique! Here is a slightly modified version:

#define SHARED_INSTANCE(...) ({\
    static dispatch_once_t pred;\
    static id sharedObject;\
    dispatch_once(&pred, ^{\
        sharedObject = (__VA_ARGS__);\
    });\
    sharedObject;\
})

It allows two forms of shared object initialization: one-line

+ (instancetype) sharedInstance {
    return SHARED_INSTANCE( [[self alloc] init] );
}

// after pre-processing:

+ (instancetype) sharedInstance {
    return ({
        static dispatch_once_t pred;
        static id sharedObject;
        dispatch_once(&pred, ^{
            sharedObject = ( [[self alloc] init] );
        });
        sharedObject;
    });
}

and multi-line (notice curly braces around the block of code):

+ (instancetype) sharedInstance {
    return SHARED_INSTANCE({
        NSLog(@"creating shared instance");
        CGFloat someValue = 84 / 2.0f;
        [[self alloc] initWithSomeValue:someValue]; // no return statement
    });
}

// after pre-processing:

+ (instancetype) sharedInstance {
    return ({
        static dispatch_once_t pred;
        static id sharedObject;
        dispatch_once(&pred, ^{
            sharedObject = ({
                NSLog(@"creating shared instance");
                CGFloat someValue = 84 / 2.0f;
                [[self alloc] initWithSomeValue:someValue];
            });
        });
        sharedObject;
    });
}

It can be used in the right part of an assignment as well:

- (void) someMethod {
    MethodPrivateHelper *helper = SHARED_INSTANCE( [[MethodPrivateHelper alloc] init] );
    // do smth with the helper
}

This modification utilizes two language features: GCC compound expressions extension, which is also supported by Clang, and C99 variadic macros support.

@julei
Copy link

julei commented Oct 21, 2014

I am afraid that in this way it can't prevent creating multiple instances by 'alloc'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment