Skip to content
This repository has been archived by the owner on Apr 29, 2021. It is now read-only.

Commit

Permalink
Fixes #12.
Browse files Browse the repository at this point in the history
  • Loading branch information
ashfurrow committed Jan 5, 2014
1 parent 9584d7d commit aecea75
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 43 deletions.
7 changes: 4 additions & 3 deletions C-41/ASHCoreDataStack.h
Expand Up @@ -10,13 +10,14 @@

@interface ASHCoreDataStack : NSObject

+ (instancetype)defaultStack;
+(instancetype)defaultStack;
+(instancetype)inMemoryStack;

@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;

- (void)saveContext;
- (void)ensureInitialLoad;
-(void)saveContext;
-(void)ensureInitialLoad;

@end
60 changes: 24 additions & 36 deletions C-41/ASHCoreDataStack.m
Expand Up @@ -22,8 +22,7 @@ @interface ASHCoreDataStack ()

@implementation ASHCoreDataStack

+ (instancetype)defaultStack
{
+(instancetype)defaultStack {
static ASHCoreDataStack *stack;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
Expand All @@ -32,8 +31,25 @@ + (instancetype)defaultStack
return stack;
}

- (void)saveContext
{
+(instancetype)inMemoryStack {
static ASHCoreDataStack *stack;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
stack = [[self alloc] init];

NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[stack managedObjectModel]];
NSError *error;
if (![persistentStoreCoordinator addPersistentStoreWithType:NSInMemoryStoreType configuration:nil URL:nil options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}

stack.persistentStoreCoordinator = persistentStoreCoordinator;
});

return stack;
}

-(void)saveContext {
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
Expand All @@ -50,8 +66,7 @@ - (void)saveContext

// Returns the managed object context for the application.
// If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
- (NSManagedObjectContext *)managedObjectContext
{
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
Expand All @@ -66,8 +81,7 @@ - (NSManagedObjectContext *)managedObjectContext

// Returns the managed object model for the application.
// If the model doesn't already exist, it is created from the application's model.
- (NSManagedObjectModel *)managedObjectModel
{
- (NSManagedObjectModel *)managedObjectModel {
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
Expand All @@ -78,8 +92,7 @@ - (NSManagedObjectModel *)managedObjectModel

// Returns the persistent store coordinator for the application.
// If the coordinator doesn't already exist, it is created and the application's store added to it.
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
Expand All @@ -89,31 +102,7 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
/*
Replace this implementation with code to handle the error appropriately.
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
Typical reasons for an error here include:
* The persistent store is not accessible;
* The schema for the persistent store is incompatible with current managed object model.
Check the error message to determine what the actual problem was.
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory.
If you encounter schema incompatibility errors during development, you can reduce their frequency by:
* Simply deleting the existing store:
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]
* Performing automatic lightweight migration by passing the following dictionary as the options parameter:
@{NSMigratePersistentStoresAutomaticallyOption:@YES, NSInferMappingModelAutomaticallyOption:@YES}
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details.
*/
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

return _persistentStoreCoordinator;
Expand All @@ -122,8 +111,7 @@ - (NSPersistentStoreCoordinator *)persistentStoreCoordinator
#pragma mark - Application's Documents directory

// Returns the URL to the application's Documents directory.
- (NSURL *)applicationDocumentsDirectory
{
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}

Expand Down
2 changes: 1 addition & 1 deletion C-41Tests/ASHDetailViewModelTests.m
Expand Up @@ -18,7 +18,7 @@
static ASHRecipe *recipe;

beforeEach(^{
NSManagedObjectContext *context = [[ASHCoreDataStack defaultStack] managedObjectContext];
NSManagedObjectContext *context = [[ASHCoreDataStack inMemoryStack] managedObjectContext];
[context reset];

recipe = setupRecipe(context);
Expand Down
2 changes: 1 addition & 1 deletion C-41Tests/ASHEditRecipeViewModelTests.m
Expand Up @@ -17,7 +17,7 @@
static ASHRecipe *recipe;

beforeEach(^{
NSManagedObjectContext *context = [[ASHCoreDataStack defaultStack] managedObjectContext];
NSManagedObjectContext *context = [[ASHCoreDataStack inMemoryStack] managedObjectContext];
[context reset];

recipe = setupRecipe(context);
Expand Down
2 changes: 1 addition & 1 deletion C-41Tests/ASHEditStepViewModelTests.m
Expand Up @@ -16,7 +16,7 @@
static ASHStep *step;

beforeEach(^{
NSManagedObjectContext *context = [[ASHCoreDataStack defaultStack] managedObjectContext];
NSManagedObjectContext *context = [[ASHCoreDataStack inMemoryStack] managedObjectContext];
[context reset];

ASHRecipe *recipe = setupRecipe(context);
Expand Down
2 changes: 1 addition & 1 deletion C-41Tests/ASHTimerViewModelTests.m
Expand Up @@ -27,7 +27,7 @@ -(void)clockTick:(NSTimer *)timer;
static ASHRecipe *recipe;

beforeEach(^{
NSManagedObjectContext *context = [[ASHCoreDataStack defaultStack] managedObjectContext];
NSManagedObjectContext *context = [[ASHCoreDataStack inMemoryStack] managedObjectContext];
[context reset];

recipe = setupRecipe(context);
Expand Down

0 comments on commit aecea75

Please sign in to comment.