Permalink
Browse files

Modifications to support disk stores and singleton entities

  • Loading branch information...
1 parent 37949aa commit 4bf08b71476e8529b5554d18a45980549edd755e Bion Oren committed Mar 26, 2012
@@ -40,6 +40,8 @@
3A803D821520057B007D0B62 /* NSManagedObject+DCAAdditions.h in Headers */ = {isa = PBXBuildFile; fileRef = 3A803D801520057B007D0B62 /* NSManagedObject+DCAAdditions.h */; settings = {ATTRIBUTES = (Public, ); }; };
3A803D831520057B007D0B62 /* NSManagedObject+DCAAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 3A803D811520057B007D0B62 /* NSManagedObject+DCAAdditions.m */; };
3A803D8415201D0E007D0B62 /* DCACacheFirstIncrementalStore.h in Headers */ = {isa = PBXBuildFile; fileRef = 3A803D50151FC7A3007D0B62 /* DCACacheFirstIncrementalStore.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ BE38A4EE1521340A005AE1E8 /* CoreDataStack+Singleton.h in Headers */ = {isa = PBXBuildFile; fileRef = BE38A4EC1521340A005AE1E8 /* CoreDataStack+Singleton.h */; settings = {ATTRIBUTES = (Public, ); }; };
+ BE38A4EF1521340A005AE1E8 /* CoreDataStack+Singleton.m in Sources */ = {isa = PBXBuildFile; fileRef = BE38A4ED1521340A005AE1E8 /* CoreDataStack+Singleton.m */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
@@ -89,6 +91,8 @@
3A803D7D152004F1007D0B62 /* DCAFetchRequest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DCAFetchRequest.m; sourceTree = "<group>"; };
3A803D801520057B007D0B62 /* NSManagedObject+DCAAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSManagedObject+DCAAdditions.h"; sourceTree = "<group>"; };
3A803D811520057B007D0B62 /* NSManagedObject+DCAAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSManagedObject+DCAAdditions.m"; sourceTree = "<group>"; };
+ BE38A4EC1521340A005AE1E8 /* CoreDataStack+Singleton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "CoreDataStack+Singleton.h"; sourceTree = "<group>"; };
+ BE38A4ED1521340A005AE1E8 /* CoreDataStack+Singleton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "CoreDataStack+Singleton.m"; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
@@ -171,6 +175,8 @@
3A35F66715202705004AEBA8 /* NSManagedObjectModel+CDHAdditions.m */,
3A0545261520464B00FE961D /* DCAFetchRequestModel.h */,
3A0545271520464B00FE961D /* DCAFetchRequestModel.m */,
+ BE38A4EC1521340A005AE1E8 /* CoreDataStack+Singleton.h */,
+ BE38A4ED1521340A005AE1E8 /* CoreDataStack+Singleton.m */,
);
path = CoreDataHelp;
sourceTree = "<group>";
@@ -225,6 +231,7 @@
3A35F66315201FC8004AEBA8 /* CoreDataHelpError.h in Headers */,
3A35F66815202705004AEBA8 /* NSManagedObjectModel+CDHAdditions.h in Headers */,
3A0545281520464B00FE961D /* DCAFetchRequestModel.h in Headers */,
+ BE38A4EE1521340A005AE1E8 /* CoreDataStack+Singleton.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -335,6 +342,7 @@
3A35F66415201FC8004AEBA8 /* CoreDataHelpError.m in Sources */,
3A35F66915202705004AEBA8 /* NSManagedObjectModel+CDHAdditions.m in Sources */,
3A0545291520464B00FE961D /* DCAFetchRequestModel.m in Sources */,
+ BE38A4EF1521340A005AE1E8 /* CoreDataStack+Singleton.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
@@ -382,6 +390,7 @@
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
+ INSTALL_PATH = "$(PRODUCT_NAME)";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
@@ -441,6 +450,7 @@
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
+ INSTALL_PATH = "$(PRODUCT_NAME)";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
@@ -459,6 +469,7 @@
GCC_WARN_ABOUT_MISSING_PROTOTYPES = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
+ INSTALL_PATH = "$(PRODUCT_NAME)";
IPHONEOS_DEPLOYMENT_TARGET = 5.0;
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
@@ -14,6 +14,7 @@
#import <CoreDataHelp/CoreDataStack.h>
#import <CoreDataHelp/CoreDataHelpError.h>
#import <CoreDataHelp/DCACacheable.h>
+#import <CoreDataHelp/CoreDataStack+Singleton.h>
void objc_retain(id x);
#define WORK_AROUND_RDAR_10732696(X) objc_retain(X)
@@ -0,0 +1,17 @@
+//
+// CoreDataStack+Singleton.h
+// mindfulmeals
+//
+// Created by Bion Oren on 3/26/12.
+// Copyright (c) 2012 DrewCrawfordApps. All rights reserved.
+//
+
+#import <CoreDataHelp/CoreDataHelp.h>
+
+typedef id (^modelInitBlock)(id model);
+
+@interface CoreDataStack (Singleton)
+
+-(id)getOrCreateFromClass:(Class)type withInit:(modelInitBlock)block;
+
+@end
@@ -0,0 +1,24 @@
+//
+// CoreDataStack+Singleton.m
+// mindfulmeals
+//
+// Created by Bion Oren on 3/26/12.
+// Copyright (c) 2012 DrewCrawfordApps. All rights reserved.
+//
+
+#import "CoreDataStack+Singleton.h"
+
+@implementation CoreDataStack (Singleton)
+
+-(id)getOrCreateFromClass:(Class)type withInit:(modelInitBlock)block {
+ DCAFetchRequest *request = [type performSelector:@selector(dcaFetchRequest)];
+ NSError *err = nil;
+ NSArray *ret = [self executeFetchRequest:request err:&err];
+ if(ret.count > 0) {
+ NSAssert(ret.count == 1, @"So much for a singleton pattern...");
+ return [ret objectAtIndex:0];
+ }
+ return block([self insertNewObjectOfClass:type]);
+}
+
+@end
@@ -9,8 +9,10 @@
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
#import <CoreDataHelp/DCAFetchRequest.h>
+
@interface CoreDataStack : NSObject
+ (CoreDataStack*) inMemoryStack;
++ (CoreDataStack*) onDiskStack;
+ (CoreDataStack*) incrementalStoreStack:(Class) autoInstallableIncrementalStore;
- (id) executeFetchRequest:(NSFetchRequest*) fetchRequest err:(NSError * __autoreleasing *) err;
@@ -38,6 +38,26 @@ + (CoreDataStack*) inMemoryStack {
return stack;
}
++ (CoreDataStack*) onDiskStack {
+ CoreDataStack *stack = [[CoreDataStack alloc] init];
+ stack->managedObjectModel = [NSManagedObjectModel defaultModel];
+ if (stack->managedObjectModel.entities.count==0) {
+ NSLog(@"Warning: There are no entities in your model. This means that the runtime environment cannot find your .xcdatamodeld file. Check that it is added to the target, or if this is a unit test, check that you are compiling with DCA_UNITTEST enabled.");
+ }
+ stack->persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:stack->managedObjectModel];
+ NSError *err = nil;
+ NSString *bundle_id = [[NSBundle mainBundle]objectForInfoDictionaryKey:@"CFBundleIdentifier"];
+ NSURL *storeUrl = [NSURL fileURLWithPath:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:bundle_id]];
+ [stack->persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:nil error:&err];
+ stack->managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSMainQueueConcurrencyType];
+ [stack->managedObjectContext setPersistentStoreCoordinator:stack->persistentStoreCoordinator];
+ if (!stack->persistentStoreCoordinator) {
+ NSLog(@"err %@",err);
+ abort();
+ }
+ return stack;
+}
+
+ (CoreDataStack*) incrementalStoreStack:(Class) autoInstallableIncrementalStore {
CoreDataStack *stack = [[CoreDataStack alloc] init];
stack->managedObjectModel = [NSManagedObjectModel defaultModel];

0 comments on commit 4bf08b7

Please sign in to comment.