Permalink
Browse files

Update Core Data code, now using DDCoreData and friends

  • Loading branch information...
coreyfloyd committed Jan 6, 2011
1 parent 06b4198 commit b12d09145949867e7d4721b1e7b514520fd99137
Showing with 185 additions and 13 deletions.
  1. +0 −2 DDCoreDataException.h
  2. +55 −7 DDCoreDataStack.h
  3. +130 −3 DDCoreDataStack.m
  4. +0 −1 FJCoreDataExtensions.h
View
@@ -22,8 +22,6 @@
* SOFTWARE.
*/
-#import <Cocoa/Cocoa.h>
-
@interface DDCoreDataException : NSException
{
View
@@ -22,7 +22,11 @@
* SOFTWARE.
*/
-#import <Cocoa/Cocoa.h>
+
+//application documents directory
+NSString* defaultStoreLocation();
+
+extern NSString* const kDefaultStoreName;
@interface DDCoreDataStack : NSObject
@@ -37,6 +41,29 @@
@property (readonly, retain) NSPersistentStoreCoordinator * coordinator;
@property (readonly, retain) NSPersistentStore * mainStore;
@property (readonly, retain) NSManagedObjectContext * mainContext;
+@property (readonly, readonly) NSURL * mainStoreURL;
+
+
+- (NSManagedObjectContext *)scratchpadContext; //autoreleased
+
+
+//convienence methods, most common way to create stores
+
+//default name, location, merging from bundles
+- (BOOL)createFullStackWithSQLiteStoreWithName:(NSString*)name; //no extension
+
+//same, but copies the store from this URL if the store doesn't exist. Use this if you have a default store that ships with the app
+- (BOOL)createFullStackWithSQLiteStoreWithName:(NSString*)name copyStoreFromURLIfNeccesary:(NSURL*)storeURL; //must have .sqlite extension
+
+//copy, will not overwrite existing store
+- (BOOL)createFullStackByCopyingStoreAtURL:(NSURL*)storeURL;
+
+//in memory store merging from bundles
+- (BOOL)createFullStackWithInMemoryStore;
+
+
+
+//full stack
- (BOOL)createFullStackWithStoreType:(NSString *)storeType
URL:(NSURL *)url
@@ -56,33 +83,54 @@
- (void)destroyFullStack;
+
+
+
+//So you want to do it all yourself?
+
+//(1) model
+
- (void)createMergedModelFromMainBundle;
- (void)createMergedModelFromBundles:(NSArray *)bundles;
- (void)destroyModel;
-- (void)createCoordinator;
+
+
+
+//(2) coordinator
+
+- (void)createCoordinator; //model must exist!!
- (void)destroyCoordinator;
+
+//(3) store
+
- (void)addMainStoreWithType:(NSString *)storeType
configuration:(NSString *)configuration
URL:(NSURL *)url
- options:(NSDictionary *)options;
+ options:(NSDictionary *)options; //coordinator must exist!!
- (BOOL)addMainStoreWithType:(NSString *)storeType
configuration:(NSString *)configuration
URL:(NSURL *)url
options:(NSDictionary *)options
- error:(NSError **)error;
+ error:(NSError **)error; //coordinator must exist!!
+
+- (void)removeMainStore; //does not remove from disk
+
+- (void)removeMainStoreDeleteFromDisk:(BOOL)flag;
-- (void)removeMainStore;
+- (void)deleteMainStoreFromDisk;
+
+//(4) context
- (void)createMainContext;
-- (void)destroyMainContext;
+- (NSManagedObjectContext*)newContext;
-- (NSManagedObjectContext *)newContext;
+- (void)destroyMainContext;
@end
View
@@ -25,6 +25,17 @@
#import "DDCoreDataStack.h"
#import "DDCoreDataException.h"
+NSString* const kDefaultStoreName = @"storedata";
+
+static NSString* const kStoreExtension = @"sqlite";
+
+NSString* defaultStoreLocation(){
+
+ return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
+
+}
+
+
@implementation DDCoreDataStack
@@ -33,12 +44,94 @@ @implementation DDCoreDataStack
@synthesize mainStore = _mainStore;
@synthesize mainContext = _mainContext;
+
- (void)dealloc
{
[self destroyFullStack];
[super dealloc];
}
+- (NSURL*)mainStoreURL{
+
+ return [[self mainStore] URL];
+
+}
+
+- (BOOL)createFullStackWithDefaultSettings{
+
+ return [self createFullStackWithSQLiteStoreWithName:kDefaultStoreName];
+}
+
+- (BOOL)createFullStackWithSQLiteStoreWithName:(NSString*)name{
+
+ NSString* fileName = [name stringByAppendingPathExtension:kStoreExtension];
+ NSString* path = [defaultStoreLocation() stringByAppendingPathComponent:fileName];
+
+ NSURL* url = [NSURL fileURLWithPath:path];
+
+ return [self createFullStackWithStoreType:NSSQLiteStoreType URL:url error:nil];
+}
+
+- (BOOL)createFullStackWithSQLiteStoreWithName:(NSString*)name copyStoreFromURLIfNeccesary:(NSURL*)storeURL{
+
+ NSString* storePathExtension = [storeURL pathExtension];
+
+ if(![storePathExtension isEqualToString:@"sqlite"])
+ return NO;
+
+ NSString* fileName = [name stringByAppendingPathExtension:kStoreExtension];
+ NSString* path = [defaultStoreLocation() stringByAppendingPathComponent:fileName];
+ NSURL* url = [NSURL fileURLWithPath:path];
+
+ if(![[NSFileManager defaultManager] fileExistsAtPath:path]){
+
+ NSError* error;
+ [[NSFileManager defaultManager] copyItemAtURL:storeURL
+ toURL:url
+ error:&error];
+
+ if(error != nil)
+ return NO;
+ }
+
+
+ return [self createFullStackWithSQLiteStoreWithName:name];
+
+}
+
+
+- (BOOL)createFullStackByCopyingStoreAtURL:(NSURL*)storeURL{
+
+ NSString* storePathExtension = [storeURL pathExtension];
+
+ if(![storePathExtension isEqualToString:@"sqlite"])
+ return NO;
+
+ NSString* fileName = [kDefaultStoreName stringByAppendingPathExtension:kStoreExtension];
+ NSString* path = [defaultStoreLocation() stringByAppendingPathComponent:fileName];
+ NSURL* url = [NSURL fileURLWithPath:path];
+
+ NSError* error;
+ [[NSFileManager defaultManager] copyItemAtURL:storeURL
+ toURL:url
+ error:&error];
+
+ if(error != nil)
+ return NO;
+
+ return [self createFullStackWithDefaultSettings];
+
+}
+
+
+
+- (BOOL)createFullStackWithInMemoryStore{
+
+ return [self createFullStackWithStoreType:NSInMemoryStoreType URL:nil error:nil];
+
+}
+
+
- (BOOL)createFullStackWithStoreType:(NSString *)storeType
URL:(NSURL *)url
error:(NSError **)error;
@@ -179,24 +272,58 @@ - (void)removeMainStore
}
}
+- (void)removeMainStoreDeleteFromDisk:(BOOL)flag{
+
+ [self deleteMainStoreFromDisk];
+
+ if (_mainStore != nil)
+ {
+ [_coordinator removePersistentStore:_mainStore error:nil];
+ [_mainStore release];
+ _mainStore = nil;
+ }
+
+}
+
- (void)createMainContext;
{
NSAssert(_mainContext == nil, @"Main context is already created");
_mainContext = [self newContext];
}
+- (NSManagedObjectContext*)newContext{
+
+ NSAssert(_coordinator != nil, @"Coordinator should not be nil");
+ NSManagedObjectContext * context = [[NSManagedObjectContext alloc] init];
+ [context setPersistentStoreCoordinator:_coordinator];
+ return context;
+
+}
+
+
- (void)destroyMainContext
{
[_mainContext release];
_mainContext = nil;
}
-- (NSManagedObjectContext *)newContext;
-{
+
+- (NSManagedObjectContext *)scratchpadContext{
+
NSAssert(_coordinator != nil, @"Coordinator should not be nil");
NSManagedObjectContext * context = [[NSManagedObjectContext alloc] init];
[context setPersistentStoreCoordinator:_coordinator];
- return context;
+ return [context autorelease];
+
}
+- (void)deleteMainStoreFromDisk{
+
+ [[NSFileManager defaultManager] removeItemAtPath:[[self mainStoreURL] path] error:nil];
+
+}
+
+
+
+
@end
View
@@ -2,6 +2,5 @@
#import <CoreData/CoreData.h>
-#import "NSManagedObjectContext+CoreDataStack.h"
#import "NSManagedObjectContext+Convenience.h"
#import "NSManagedObject+Extensions.h"

0 comments on commit b12d091

Please sign in to comment.