Last active
December 30, 2015 12:39
-
-
Save netshade/7830683 to your computer and use it in GitHub Desktop.
Objective C double locking singleton example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PCSingletonExample.h | |
// Lab11 | |
// | |
// Created by Chris Zelenak on 12/6/13. | |
// Copyright (c) 2013 Bootstrapping iOS. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <CoreData/CoreData.h> | |
@interface CoreDataLayer : NSObject | |
+(CoreDataLayer *) sharedInstance; | |
@property NSManagedObjectContext * context; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// PCSingletonExample.m | |
// Lab11 | |
// | |
// Created by Chris Zelenak on 12/6/13. | |
// Copyright (c) 2013 Bootstrapping iOS. All rights reserved. | |
// | |
#import "PCSingletonExample.h" | |
@implementation CoreDataLayer | |
+(CoreDataLayer *) sharedInstance { | |
static CoreDataLayer * _inst; | |
if(_inst == nil){ | |
@synchronized([self class]){ | |
if(_inst == nil){ | |
_inst = [[CoreDataLayer alloc] init]; | |
} | |
} | |
} | |
return _inst; | |
} | |
-(id) init{ | |
self = [super init]; | |
if(self){ | |
self.context = nil; // some core data setup | |
} | |
return self; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment