Skip to content

Instantly share code, notes, and snippets.

@netshade
Last active December 30, 2015 12:39
Show Gist options
  • Save netshade/7830683 to your computer and use it in GitHub Desktop.
Save netshade/7830683 to your computer and use it in GitHub Desktop.
Objective C double locking singleton example
//
// 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
//
// 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