Skip to content

Instantly share code, notes, and snippets.

@xjones
Forked from lukeredpath/ExampleClass.m
Created October 21, 2011 21:59
Show Gist options
  • Select an option

  • Save xjones/1305095 to your computer and use it in GitHub Desktop.

Select an option

Save xjones/1305095 to your computer and use it in GitHub Desktop.

Revisions

  1. xjones revised this gist Oct 21, 2011. 2 changed files with 55 additions and 6 deletions.
    6 changes: 6 additions & 0 deletions ExampleClass.m
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,11 @@
    @implementation MySharedThing

    // Using method 1
    //
    DEFINE_SHARED_INSTANCE_FOR_CLASS(MySharedThing)

    // Using method 2
    //
    + (id)sharedInstance
    {
    DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
    55 changes: 49 additions & 6 deletions GCDSingleton.h
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,50 @@
    //
    // GCDSingleton.h
    //
    // Macros to simplify creating of a sharedInstance for a class using GCD (ARC compliant)
    //
    // Use one of the following methods to create a sharedInstance
    //

    //
    // METHOD #1: use this if you want the sharedInstance method to be 'sharedClassName'
    //
    // @implementation MySharedThing
    //
    // DEFINE_SHARED_INSTANCE_FOR_CLASS(MySharedThing)
    //

    #define DEFINE_SHARED_INSTANCE_FOR_CLASS(className) \
    + (id)shared##className \
    { \
    static dispatch_once_t pred = 0; \
    __strong static id _sharedObject = nil; \
    dispatch_once(&pred, ^{ \
    _sharedObject = ^{return [[self alloc] init];}(); \
    }); \
    return _sharedObject; \
    } \


    //
    // METHOD #2: use this if you want to name the 'sharedXXX' method yourself
    //
    // @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; \
    static dispatch_once_t pred = 0; \
    __strong static id _sharedObject = nil; \
    dispatch_once(&pred, ^{ \
    _sharedObject = block(); \
    }); \
    return _sharedObject; \
  2. @lukeredpath lukeredpath created this gist Jun 30, 2011.
    10 changes: 10 additions & 0 deletions ExampleClass.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    @implementation MySharedThing

    + (id)sharedInstance
    {
    DEFINE_SHARED_INSTANCE_USING_BLOCK(^{
    return [[self alloc] init];
    });
    }

    @end
    7 changes: 7 additions & 0 deletions GCDSingleton.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    #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; \