-
-
Save wagpinto/ad0799c561d33680b663 to your computer and use it in GitHub Desktop.
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
// | |
// Stack.m | |
// | |
// Created by Joshua Howland on 6/12/14. | |
// Copyright (c) 2014 DevMountain. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@import CoreData; | |
@interface Stack : NSObject | |
+ (Stack *)sharedInstance; | |
@property (nonatomic, strong, readonly) NSManagedObjectContext *managedObjectContext; | |
@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
// | |
// Stack.m | |
// | |
// Created by Joshua Howland on 6/12/14. | |
// Copyright (c) 2014 DevMountain. All rights reserved. | |
// | |
#import "Stack.h" | |
@interface Stack () | |
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext; | |
@end | |
@implementation Stack | |
+ (Stack *)sharedInstance { | |
static Stack *sharedInstance = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
sharedInstance = [[Stack alloc] init]; | |
}); | |
return sharedInstance; | |
} | |
- (id)init { | |
self = [super init]; | |
if (self) { | |
[self setupManagedObjectContext]; | |
} | |
return self; | |
} | |
- (void)setupManagedObjectContext | |
{ | |
self.managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType]; | |
self.managedObjectContext.persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel]; | |
NSError* error; | |
[self.managedObjectContext.persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType | |
configuration:nil | |
URL:self.storeURL | |
options:nil | |
error:&error]; | |
if (error) { | |
NSLog(@"error: %@", error); | |
} | |
self.managedObjectContext.undoManager = [[NSUndoManager alloc] init]; | |
} | |
- (NSURL*)storeURL | |
{ | |
NSURL* documentsDirectory = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:NULL]; | |
return [documentsDirectory URLByAppendingPathComponent:@"db.sqlite"]; | |
} | |
- (NSURL*)modelURL | |
{ | |
return [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"]; | |
} | |
- (NSManagedObjectModel*)managedObjectModel | |
{ | |
return [[NSManagedObjectModel alloc] initWithContentsOfURL:self.modelURL]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment