Permalink
Browse files

Tidy up and add commentry

  • Loading branch information...
danielctull committed Jul 24, 2012
1 parent 8c3a76b commit 1ab05415f80c15002688495bba68cd11a786757d
@@ -413,7 +413,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Demo/Demo-Prefix.pch";
INFOPLIST_FILE = "Demo/Demo-Info.plist";
- IPHONEOS_DEPLOYMENT_TARGET = 6.0;
+ IPHONEOS_DEPLOYMENT_TARGET = 5.0;
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = CoreDataStack;
WRAPPER_EXTENSION = app;
@@ -428,7 +428,7 @@
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "Demo/Demo-Prefix.pch";
INFOPLIST_FILE = "Demo/Demo-Info.plist";
- IPHONEOS_DEPLOYMENT_TARGET = 6.0;
+ IPHONEOS_DEPLOYMENT_TARGET = 5.0;
OTHER_CFLAGS = "-DNS_BLOCK_ASSERTIONS=1";
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = CoreDataStack;
@@ -46,7 +46,8 @@
// 4) On iOS, the app entering the background, or terminating, automatically triggers a save if needed. You can be notified of such saves by setting the .automaticSaveCompletionHandler property
//
// 5) On iOS, when saving on a background thread, the stack protects against termination/suspension by declaring the save as a background task
-
+//
+// 6) Generation of private queue managed object contexts via -[DCTCoreDataStack newWorkerManagedObjectContext]. Saving this causes a save to disk and a merge to the context in managedObjectContext
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>
@@ -86,8 +87,6 @@ typedef BOOL (^DCTCoreDataStackPersistentStoreErrorResolutionBlock) (NSError *er
*/
- (NSManagedObjectContext *)newWorkerManagedObjectContext;
-@property (nonatomic, strong, readonly) NSManagedObjectModel *managedObjectModel;
-
@property (nonatomic, copy, readonly) NSURL *storeURL;
@property (nonatomic, copy, readonly) NSString *storeType;
@property (nonatomic, copy, readonly) NSDictionary *storeOptions;
@@ -44,34 +44,34 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
@interface DCTCoreDataStack ()
@property (nonatomic, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
+@property (nonatomic, readonly) NSManagedObjectModel *managedObjectModel;
@end
@implementation DCTCoreDataStack {
__strong NSManagedObjectContext *_managedObjectContext;
- __strong NSManagedObjectModel *managedObjectModel;
- __strong NSPersistentStoreCoordinator *persistentStoreCoordinator;
+ __strong NSManagedObjectModel *_managedObjectModel;
+ __strong NSPersistentStoreCoordinator *_persistentStoreCoordinator;
__strong NSManagedObjectContext *_rootContext;
}
#pragma mark - NSObject
-#ifdef TARGET_OS_IPHONE
- (void)dealloc {
- UIApplication *app = [UIApplication sharedApplication];
NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
+ [defaultCenter removeObserver:self
+ name:NSManagedObjectContextDidSaveNotification
+ object:_rootContext];
+
+#ifdef TARGET_OS_IPHONE
+ UIApplication *app = [UIApplication sharedApplication];
[defaultCenter removeObserver:self
name:UIApplicationDidEnterBackgroundNotification
object:app];
-
[defaultCenter removeObserver:self
name:UIApplicationWillTerminateNotification
object:app];
-
- [defaultCenter removeObserver:self
- name:NSManagedObjectContextDidSaveNotification
- object:_rootContext];
-}
#endif
+}
#pragma mark - Initialization
@@ -113,12 +113,12 @@ - (id)initWithStoreURL:(NSURL *)storeURL
UIApplication *app = [UIApplication sharedApplication];
[defaultCenter addObserver:self
- selector:@selector(dctInternal_applicationDidEnterBackgroundNotification:)
+ selector:@selector(_applicationDidEnterBackgroundNotification:)
name:UIApplicationDidEnterBackgroundNotification
object:app];
[defaultCenter addObserver:self
- selector:@selector(dctInternal_applicationWillTerminateNotification:)
+ selector:@selector(_applicationWillTerminateNotification:)
name:UIApplicationWillTerminateNotification
object:app];
#endif
@@ -127,7 +127,7 @@ - (id)initWithStoreURL:(NSURL *)storeURL
}
- (id)initWithStoreFilename:(NSString *)filename {
- NSURL *storeURL = [[[self class] dctInternal_applicationDocumentsDirectory] URLByAppendingPathComponent:filename];
+ NSURL *storeURL = [[[self class] _applicationDocumentsDirectory] URLByAppendingPathComponent:filename];
return [self initWithStoreURL:storeURL
storeType:NSSQLiteStoreType
storeOptions:nil
@@ -137,92 +137,91 @@ - (id)initWithStoreFilename:(NSString *)filename {
#pragma mark - Getters
-- (void)_rootContextDidSaveNotification:(NSNotification *)notification {
- [self.managedObjectContext performBlock:^{
- [self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
- }];
-}
-
- (NSManagedObjectContext *)newWorkerManagedObjectContext {
- return [self _loadManagedObjectContextWithName:@"DCTCoreDataStack.workerContext"
- concurrencyType:NSPrivateQueueConcurrencyType];
+ return [self _newManagedObjectContextWithName:@"DCTCoreDataStack.workerContext"
+ concurrencyType:NSPrivateQueueConcurrencyType];
}
- (NSManagedObjectContext *)managedObjectContext {
if (_managedObjectContext == nil)
- _managedObjectContext = [self _loadManagedObjectContextWithName:@"DCTCoreDataStack.mainContext"
- concurrencyType:NSMainQueueConcurrencyType];
+ _managedObjectContext = [self _newManagedObjectContextWithName:@"DCTCoreDataStack.mainContext"
+ concurrencyType:NSMainQueueConcurrencyType];
return _managedObjectContext;
}
- (NSManagedObjectModel *)managedObjectModel {
- if (managedObjectModel == nil)
- [self dctInternal_loadManagedObjectModel];
+ if (_managedObjectModel == nil)
+ [self _loadManagedObjectModel];
- return managedObjectModel;
+ return _managedObjectModel;
}
#pragma mark - Internal Loading
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
- if (persistentStoreCoordinator == nil)
- [self dctInternal_loadPersistentStoreCoordinator];
+ if (_persistentStoreCoordinator == nil)
+ [self _loadPersistentStoreCoordinator];
- return persistentStoreCoordinator;
+ return _persistentStoreCoordinator;
}
-- (NSManagedObjectContext *)_loadManagedObjectContextWithName:(NSString *)name
- concurrencyType:(NSManagedObjectContextConcurrencyType)concurrencyType {
+- (NSManagedObjectContext *)_newManagedObjectContextWithName:(NSString *)name
+ concurrencyType:(NSManagedObjectContextConcurrencyType)concurrencyType {
NSManagedObjectContext *managedObjectContext = [[_DCTCDSManagedObjectContext alloc] initWithConcurrencyType:concurrencyType];
[managedObjectContext setParentContext:_rootContext];
managedObjectContext.dct_name = name;
return managedObjectContext;
}
-- (void)dctInternal_loadManagedObjectModel {
+- (void)_loadManagedObjectModel {
- if (self.modelURL) {
- managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:self.modelURL];
- } else {
- managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:[NSBundle mainBundle]]];
- }
+ if (self.modelURL)
+ _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:self.modelURL];
+ else
+ _managedObjectModel = [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:[NSBundle mainBundle]]];
}
-- (void)dctInternal_loadPersistentStoreCoordinator {
+- (void)_loadPersistentStoreCoordinator {
- persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
+ _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.managedObjectModel];
NSError *error = nil;
- NSPersistentStore *persistentStore = [persistentStoreCoordinator addPersistentStoreWithType:self.storeType
- configuration:self.modelConfiguration
- URL:self.storeURL
- options:self.storeOptions
- error:&error];
+ NSPersistentStore *persistentStore = [_persistentStoreCoordinator addPersistentStoreWithType:self.storeType
+ configuration:self.modelConfiguration
+ URL:self.storeURL
+ options:self.storeOptions
+ error:&error];
if (!persistentStore && self.didResolvePersistentStoreErrorHandler) {
if (self.didResolvePersistentStoreErrorHandler(error))
- [persistentStoreCoordinator addPersistentStoreWithType:self.storeType
- configuration:self.modelConfiguration
- URL:self.storeURL
- options:self.storeOptions
- error:NULL];
+ [_persistentStoreCoordinator addPersistentStoreWithType:self.storeType
+ configuration:self.modelConfiguration
+ URL:self.storeURL
+ options:self.storeOptions
+ error:NULL];
}
}
#pragma mark - Other Internal
-+ (NSURL *)dctInternal_applicationDocumentsDirectory {
+- (void)_rootContextDidSaveNotification:(NSNotification *)notification {
+ [self.managedObjectContext performBlock:^{
+ [self.managedObjectContext mergeChangesFromContextDidSaveNotification:notification];
+ }];
+}
+
++ (NSURL *)_applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
#ifdef TARGET_OS_IPHONE
-- (void)dctInternal_applicationDidEnterBackgroundNotification:(NSNotification *)notification {
+- (void)_applicationDidEnterBackgroundNotification:(NSNotification *)notification {
NSManagedObjectContext *context = self.managedObjectContext;
@@ -235,7 +234,7 @@ - (void)dctInternal_applicationDidEnterBackgroundNotification:(NSNotification *)
// TODO: what if there was a save error?
}
-- (void)dctInternal_applicationWillTerminateNotification:(NSNotification *)notification {
+- (void)_applicationWillTerminateNotification:(NSNotification *)notification {
NSManagedObjectContext *context = self.managedObjectContext;

0 comments on commit 1ab0541

Please sign in to comment.