Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Update Core Data code, now using DDCoreData and friends

  • Loading branch information...
commit b12d09145949867e7d4721b1e7b514520fd99137 1 parent 06b4198
Corey Floyd authored January 06, 2011
2  DDCoreDataException.h
@@ -22,8 +22,6 @@
22 22
  * SOFTWARE.
23 23
  */
24 24
 
25  
-#import <Cocoa/Cocoa.h>
26  
-
27 25
 
28 26
 @interface DDCoreDataException : NSException
29 27
 {
62  DDCoreDataStack.h
@@ -22,7 +22,11 @@
22 22
  * SOFTWARE.
23 23
  */
24 24
 
25  
-#import <Cocoa/Cocoa.h>
  25
+
  26
+//application documents directory
  27
+NSString* defaultStoreLocation();
  28
+
  29
+extern NSString* const kDefaultStoreName; 
26 30
 
27 31
 
28 32
 @interface DDCoreDataStack : NSObject
@@ -37,6 +41,29 @@
37 41
 @property (readonly, retain) NSPersistentStoreCoordinator * coordinator;
38 42
 @property (readonly, retain) NSPersistentStore * mainStore;
39 43
 @property (readonly, retain) NSManagedObjectContext * mainContext;
  44
+@property (readonly, readonly) NSURL * mainStoreURL;
  45
+
  46
+
  47
+- (NSManagedObjectContext *)scratchpadContext; //autoreleased
  48
+
  49
+
  50
+//convienence methods, most common way to create stores
  51
+
  52
+//default name, location, merging from bundles
  53
+- (BOOL)createFullStackWithSQLiteStoreWithName:(NSString*)name; //no extension
  54
+
  55
+//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
  56
+- (BOOL)createFullStackWithSQLiteStoreWithName:(NSString*)name copyStoreFromURLIfNeccesary:(NSURL*)storeURL; //must have .sqlite extension
  57
+
  58
+//copy, will not overwrite existing store
  59
+- (BOOL)createFullStackByCopyingStoreAtURL:(NSURL*)storeURL;
  60
+
  61
+//in memory store merging from bundles
  62
+- (BOOL)createFullStackWithInMemoryStore;
  63
+
  64
+
  65
+
  66
+//full stack
40 67
 
41 68
 - (BOOL)createFullStackWithStoreType:(NSString *)storeType
42 69
                                  URL:(NSURL *)url
@@ -56,33 +83,54 @@
56 83
 
57 84
 - (void)destroyFullStack;
58 85
 
  86
+
  87
+
  88
+
  89
+//So you want to do it all yourself?
  90
+
  91
+//(1) model
  92
+
59 93
 - (void)createMergedModelFromMainBundle;
60 94
 
61 95
 - (void)createMergedModelFromBundles:(NSArray *)bundles;
62 96
 
63 97
 - (void)destroyModel;
64 98
 
65  
-- (void)createCoordinator;
  99
+
  100
+
  101
+
  102
+//(2) coordinator
  103
+
  104
+- (void)createCoordinator; //model must exist!!
66 105
 
67 106
 - (void)destroyCoordinator;
68 107
 
  108
+
  109
+//(3) store
  110
+
69 111
 - (void)addMainStoreWithType:(NSString *)storeType
70 112
                configuration:(NSString *)configuration
71 113
                          URL:(NSURL *)url
72  
-                     options:(NSDictionary *)options;
  114
+                     options:(NSDictionary *)options; //coordinator must exist!!
73 115
 
74 116
 - (BOOL)addMainStoreWithType:(NSString *)storeType
75 117
                configuration:(NSString *)configuration
76 118
                          URL:(NSURL *)url
77 119
                      options:(NSDictionary *)options
78  
-                       error:(NSError **)error;
  120
+                       error:(NSError **)error; //coordinator must exist!!
  121
+
  122
+- (void)removeMainStore; //does not remove from disk
  123
+
  124
+- (void)removeMainStoreDeleteFromDisk:(BOOL)flag;
79 125
 
80  
-- (void)removeMainStore;
  126
+- (void)deleteMainStoreFromDisk;
  127
+
  128
+//(4) context
81 129
 
82 130
 - (void)createMainContext;
83 131
 
84  
-- (void)destroyMainContext;
  132
+- (NSManagedObjectContext*)newContext;
85 133
 
86  
-- (NSManagedObjectContext *)newContext;
  134
+- (void)destroyMainContext;
87 135
 
88 136
 @end
133  DDCoreDataStack.m
@@ -25,6 +25,17 @@
25 25
 #import "DDCoreDataStack.h"
26 26
 #import "DDCoreDataException.h"
27 27
 
  28
+NSString* const kDefaultStoreName = @"storedata";
  29
+
  30
+static NSString* const kStoreExtension = @"sqlite";
  31
+
  32
+NSString* defaultStoreLocation(){
  33
+    
  34
+    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  35
+
  36
+}
  37
+
  38
+
28 39
 
29 40
 @implementation DDCoreDataStack
30 41
 
@@ -33,12 +44,94 @@ @implementation DDCoreDataStack
33 44
 @synthesize mainStore = _mainStore;
34 45
 @synthesize mainContext = _mainContext;
35 46
 
  47
+
36 48
 - (void)dealloc
37 49
 {
38 50
     [self destroyFullStack];
39 51
     [super dealloc];
40 52
 }
41 53
 
  54
+- (NSURL*)mainStoreURL{
  55
+    
  56
+    return [[self mainStore] URL];
  57
+    
  58
+}
  59
+
  60
+- (BOOL)createFullStackWithDefaultSettings{
  61
+    
  62
+    return [self createFullStackWithSQLiteStoreWithName:kDefaultStoreName];
  63
+}
  64
+
  65
+- (BOOL)createFullStackWithSQLiteStoreWithName:(NSString*)name{
  66
+    
  67
+    NSString* fileName = [name stringByAppendingPathExtension:kStoreExtension];
  68
+    NSString* path = [defaultStoreLocation() stringByAppendingPathComponent:fileName];
  69
+    
  70
+    NSURL* url = [NSURL fileURLWithPath:path];
  71
+    
  72
+    return [self createFullStackWithStoreType:NSSQLiteStoreType URL:url error:nil];
  73
+}
  74
+
  75
+- (BOOL)createFullStackWithSQLiteStoreWithName:(NSString*)name copyStoreFromURLIfNeccesary:(NSURL*)storeURL{
  76
+    
  77
+    NSString* storePathExtension = [storeURL pathExtension];
  78
+    
  79
+	if(![storePathExtension isEqualToString:@"sqlite"])
  80
+        return NO;
  81
+    
  82
+    NSString* fileName = [name stringByAppendingPathExtension:kStoreExtension];
  83
+    NSString* path = [defaultStoreLocation() stringByAppendingPathComponent:fileName];
  84
+    NSURL* url = [NSURL fileURLWithPath:path];
  85
+
  86
+    if(![[NSFileManager defaultManager] fileExistsAtPath:path]){
  87
+        
  88
+        NSError* error;
  89
+        [[NSFileManager defaultManager] copyItemAtURL:storeURL 
  90
+                                                toURL:url
  91
+                                                 error:&error];
  92
+        
  93
+        if(error != nil)
  94
+            return NO;
  95
+    }
  96
+    
  97
+    
  98
+    return [self createFullStackWithSQLiteStoreWithName:name];
  99
+    
  100
+}
  101
+
  102
+
  103
+- (BOOL)createFullStackByCopyingStoreAtURL:(NSURL*)storeURL{
  104
+    
  105
+    NSString* storePathExtension = [storeURL pathExtension];
  106
+    
  107
+	if(![storePathExtension isEqualToString:@"sqlite"])
  108
+        return NO;
  109
+    
  110
+    NSString* fileName = [kDefaultStoreName stringByAppendingPathExtension:kStoreExtension];
  111
+    NSString* path = [defaultStoreLocation() stringByAppendingPathComponent:fileName];
  112
+    NSURL* url = [NSURL fileURLWithPath:path];
  113
+    
  114
+    NSError* error;
  115
+    [[NSFileManager defaultManager] copyItemAtURL:storeURL 
  116
+                                            toURL:url
  117
+                                            error:&error];
  118
+    
  119
+    if(error != nil)
  120
+        return NO;
  121
+    
  122
+    return [self createFullStackWithDefaultSettings];
  123
+    
  124
+}
  125
+
  126
+       
  127
+
  128
+- (BOOL)createFullStackWithInMemoryStore{
  129
+        
  130
+    return [self createFullStackWithStoreType:NSInMemoryStoreType URL:nil error:nil];
  131
+    
  132
+}
  133
+
  134
+
42 135
 - (BOOL)createFullStackWithStoreType:(NSString *)storeType
43 136
                                  URL:(NSURL *)url
44 137
                                error:(NSError **)error;
@@ -179,24 +272,58 @@ - (void)removeMainStore
179 272
     }
180 273
 }
181 274
 
  275
+- (void)removeMainStoreDeleteFromDisk:(BOOL)flag{
  276
+
  277
+    [self deleteMainStoreFromDisk];
  278
+   
  279
+    if (_mainStore != nil)
  280
+    {
  281
+        [_coordinator removePersistentStore:_mainStore error:nil];
  282
+        [_mainStore release];
  283
+        _mainStore = nil;
  284
+    }
  285
+    
  286
+}
  287
+
182 288
 - (void)createMainContext;
183 289
 {
184 290
     NSAssert(_mainContext == nil, @"Main context is already created");
185 291
     _mainContext = [self newContext];
186 292
 }
187 293
 
  294
+- (NSManagedObjectContext*)newContext{
  295
+    
  296
+    NSAssert(_coordinator != nil, @"Coordinator should not be nil");
  297
+    NSManagedObjectContext * context = [[NSManagedObjectContext alloc] init];
  298
+    [context setPersistentStoreCoordinator:_coordinator];
  299
+    return context;
  300
+    
  301
+}
  302
+
  303
+
188 304
 - (void)destroyMainContext
189 305
 {
190 306
     [_mainContext release];
191 307
     _mainContext = nil;
192 308
 }
193 309
 
194  
-- (NSManagedObjectContext *)newContext;
195  
-{
  310
+
  311
+- (NSManagedObjectContext *)scratchpadContext{
  312
+	
196 313
     NSAssert(_coordinator != nil, @"Coordinator should not be nil");
197 314
     NSManagedObjectContext * context = [[NSManagedObjectContext alloc] init];
198 315
     [context setPersistentStoreCoordinator:_coordinator];
199  
-    return context;
  316
+    return [context autorelease];
  317
+    
200 318
 }
201 319
 
  320
+- (void)deleteMainStoreFromDisk{
  321
+    
  322
+    [[NSFileManager defaultManager] removeItemAtPath:[[self mainStoreURL] path] error:nil];
  323
+    
  324
+}
  325
+
  326
+
  327
+
  328
+
202 329
 @end
1  FJCoreDataExtensions.h
@@ -2,6 +2,5 @@
2 2
 
3 3
 #import <CoreData/CoreData.h>
4 4
 
5  
-#import "NSManagedObjectContext+CoreDataStack.h"
6 5
 #import "NSManagedObjectContext+Convenience.h"
7 6
 #import "NSManagedObject+Extensions.h"

0 notes on commit b12d091

Please sign in to comment.
Something went wrong with that request. Please try again.