Skip to content

Instantly share code, notes, and snippets.

@trm36
Forked from jkhowland/Stack.h
Last active August 29, 2015 14:18
Show Gist options
  • Select an option

  • Save trm36/26f01bf66b90d1cc0ab5 to your computer and use it in GitHub Desktop.

Select an option

Save trm36/26f01bf66b90d1cc0ab5 to your computer and use it in GitHub Desktop.

Revisions

  1. @jkhowland jkhowland revised this gist Oct 2, 2014. 2 changed files with 35 additions and 7 deletions.
    17 changes: 17 additions & 0 deletions Stack.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    //
    // Stack.m
    //
    // Created by Joshua Howland on 6/12/14.
    // Copyright (c) 2014 DevMountain. All rights reserved.
    //


    #import <Foundation/Foundation.h>

    @interface Stack : NSObject

    + (Stack *)sharedInstance;

    @property (nonatomic, strong, readonly) NSManagedObjectContext *managedObjectContext;

    @end
    25 changes: 18 additions & 7 deletions DBStack.m → Stack.m
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,29 @@
    //
    // DBStack.m
    // Core Data Bank
    // Stack.m
    //
    // Created by Joshua Howland on 6/12/14.
    // Copyright (c) 2014 DevMountain. All rights reserved.
    //

    #import "DBStack.h"
    #import "Stack.h"

    @interface DBStack ()
    @interface Stack ()

    @property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;

    @end

    @implementation DBStack
    @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];
    @@ -28,6 +37,7 @@ - (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
    @@ -37,6 +47,7 @@ - (void)setupManagedObjectContext
    if (error) {
    NSLog(@"error: %@", error);
    }

    self.managedObjectContext.undoManager = [[NSUndoManager alloc] init];
    }

    @@ -48,12 +59,12 @@ - (NSURL*)storeURL

    - (NSURL*)modelURL
    {
    return [[NSBundle mainBundle] URLForResource:@"CoreDataBank" withExtension:@"momd"];
    return [[NSBundle mainBundle] URLForResource:@"Model" withExtension:@"momd"];
    }

    - (NSManagedObjectModel*)managedObjectModel
    {
    return [[NSManagedObjectModel alloc] initWithContentsOfURL:self.modelURL];
    }

    @end
    @end
  2. @jkhowland jkhowland revised this gist Jun 12, 2014. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions DBStack.m
    Original file line number Diff line number Diff line change
    @@ -16,6 +16,14 @@ @interface DBStack ()

    @implementation DBStack

    - (id)init {
    self = [super init];
    if (self) {
    [self setupManagedObjectContext];
    }
    return self;
    }

    - (void)setupManagedObjectContext
    {
    self.managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
  3. @jkhowland jkhowland revised this gist Jun 12, 2014. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions DBStack.m
    Original file line number Diff line number Diff line change
    @@ -47,3 +47,5 @@ - (NSManagedObjectModel*)managedObjectModel
    {
    return [[NSManagedObjectModel alloc] initWithContentsOfURL:self.modelURL];
    }

    @end
  4. @jkhowland jkhowland created this gist Jun 12, 2014.
    49 changes: 49 additions & 0 deletions DBStack.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    //
    // DBStack.m
    // Core Data Bank
    //
    // Created by Joshua Howland on 6/12/14.
    // Copyright (c) 2014 DevMountain. All rights reserved.
    //

    #import "DBStack.h"

    @interface DBStack ()

    @property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;

    @end

    @implementation DBStack

    - (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:@"CoreDataBank" withExtension:@"momd"];
    }

    - (NSManagedObjectModel*)managedObjectModel
    {
    return [[NSManagedObjectModel alloc] initWithContentsOfURL:self.modelURL];
    }