-
-
Save phillbaker/3286506 to your computer and use it in GitHub Desktop.
Revisions
-
phillbaker revised this gist
Aug 7, 2012 . 1 changed file with 15 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -69,6 +69,21 @@ - (NSPersistentStoreCoordinator*)persistentStoreCoordinator { URL:storeURL options:options error:&error]) { /* TODO: Replace this implementation with code to handle the error appropriately. ... 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: [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; */ NSLog(@"Fatal error while creating persistent store: %@", error); abort(); } -
rojotek revised this gist
Apr 11, 2012 . 1 changed file with 4 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -30,12 +30,6 @@ + (DataManager*)sharedInstance { - (void)dealloc { [self save]; } - (NSManagedObjectModel*)objectModel { @@ -122,8 +116,8 @@ - (NSString*)sharedDocumentsPath { return SharedDocumentsPath; // Compose a path to the <Library>/Database directory NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]; SharedDocumentsPath = [libraryPath stringByAppendingPathComponent:@"Database"]; // Ensure the database directory exists NSFileManager *manager = [NSFileManager defaultManager]; @@ -144,10 +138,10 @@ - (NSString*)sharedDocumentsPath { } - (NSManagedObjectContext*)managedObjectContext { NSManagedObjectContext *ctx = [[NSManagedObjectContext alloc] init]; [ctx setPersistentStoreCoordinator:self.persistentStoreCoordinator]; return ctx; } @end -
AlexNachbaur revised this gist
Apr 20, 2011 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -31,9 +31,9 @@ + (DataManager*)sharedInstance { - (void)dealloc { [self save]; [_persistentStoreCoordinator release]; [_mainObjectContext release]; [_objectModel release]; [super dealloc]; } -
AlexNachbaur revised this gist
Apr 15, 2011 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -130,9 +130,11 @@ - (NSString*)sharedDocumentsPath { BOOL isDirectory; if (![manager fileExistsAtPath:SharedDocumentsPath isDirectory:&isDirectory] || !isDirectory) { NSError *error = nil; NSDictionary *attr = [NSDictionary dictionaryWithObject:NSFileProtectionComplete forKey:NSFileProtectionKey]; [manager createDirectoryAtPath:SharedDocumentsPath withIntermediateDirectories:YES attributes:attr error:&error]; if (error) NSLog(@"Error creating directory path: %@", [error localizedDescription]); -
AlexNachbaur created this gist
Apr 15, 2011 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ // DataManager.h #import <Foundation/Foundation.h> #import <CoreData/CoreData.h> extern NSString * const DataManagerDidSaveNotification; extern NSString * const DataManagerDidSaveFailedNotification; @interface DataManager : NSObject { } @property (nonatomic, readonly, retain) NSManagedObjectModel *objectModel; @property (nonatomic, readonly, retain) NSManagedObjectContext *mainObjectContext; @property (nonatomic, readonly, retain) NSPersistentStoreCoordinator *persistentStoreCoordinator; + (DataManager*)sharedInstance; - (BOOL)save; - (NSManagedObjectContext*)managedObjectContext; @end This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,151 @@ // DataManager.m #import "DataManager.h" NSString * const DataManagerDidSaveNotification = @"DataManagerDidSaveNotification"; NSString * const DataManagerDidSaveFailedNotification = @"DataManagerDidSaveFailedNotification"; @interface DataManager () - (NSString*)sharedDocumentsPath; @end @implementation DataManager @synthesize persistentStoreCoordinator = _persistentStoreCoordinator; @synthesize mainObjectContext = _mainObjectContext; @synthesize objectModel = _objectModel; NSString * const kDataManagerBundleName = @"MyApp"; NSString * const kDataManagerModelName = @"MyApp"; NSString * const kDataManagerSQLiteName = @"MyApp.sqlite"; + (DataManager*)sharedInstance { static dispatch_once_t pred; static DataManager *sharedInstance = nil; dispatch_once(&pred, ^{ sharedInstance = [[self alloc] init]; }); return sharedInstance; } - (void)dealloc { [self save]; self.persistentStoreCoordinator = nil; self.mainObjectContext = nil; self.objectModel = nil; [super dealloc]; } - (NSManagedObjectModel*)objectModel { if (_objectModel) return _objectModel; NSBundle *bundle = [NSBundle mainBundle]; if (kDataManagerBundleName) { NSString *bundlePath = [[NSBundle mainBundle] pathForResource:kDataManagerBundleName ofType:@"bundle"]; bundle = [NSBundle bundleWithPath:bundlePath]; } NSString *modelPath = [bundle pathForResource:kDataManagerModelName ofType:@"momd"]; _objectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:[NSURL fileURLWithPath:modelPath]]; return _objectModel; } - (NSPersistentStoreCoordinator*)persistentStoreCoordinator { if (_persistentStoreCoordinator) return _persistentStoreCoordinator; // Get the paths to the SQLite file NSString *storePath = [[self sharedDocumentsPath] stringByAppendingPathComponent:kDataManagerSQLiteName]; NSURL *storeURL = [NSURL fileURLWithPath:storePath]; // Define the Core Data version migration options NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; // Attempt to load the persistent store NSError *error = nil; _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:self.objectModel]; if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { NSLog(@"Fatal error while creating persistent store: %@", error); abort(); } return _persistentStoreCoordinator; } - (NSManagedObjectContext*)mainObjectContext { if (_mainObjectContext) return _mainObjectContext; // Create the main context only on the main thread if (![NSThread isMainThread]) { [self performSelectorOnMainThread:@selector(mainObjectContext) withObject:nil waitUntilDone:YES]; return _mainObjectContext; } _mainObjectContext = [[NSManagedObjectContext alloc] init]; [_mainObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator]; return _mainObjectContext; } - (BOOL)save { if (![self.mainObjectContext hasChanges]) return YES; NSError *error = nil; if (![self.mainObjectContext save:&error]) { NSLog(@"Error while saving: %@\n%@", [error localizedDescription], [error userInfo]); [[NSNotificationCenter defaultCenter] postNotificationName:DataManagerDidSaveFailedNotification object:error]; return NO; } [[NSNotificationCenter defaultCenter] postNotificationName:DataManagerDidSaveNotification object:nil]; return YES; } - (NSString*)sharedDocumentsPath { static NSString *SharedDocumentsPath = nil; if (SharedDocumentsPath) return SharedDocumentsPath; // Compose a path to the <Library>/Database directory NSString *libraryPath = [[NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0] retain]; SharedDocumentsPath = [[libraryPath stringByAppendingPathComponent:@"Database"] retain]; // Ensure the database directory exists NSFileManager *manager = [NSFileManager defaultManager]; BOOL isDirectory; if (![manager fileExistsAtPath:SharedDocumentsPath isDirectory:&isDirectory] || !isDirectory) { NSError *error = nil; [manager createDirectoryAtPath:SharedDocumentsPath withIntermediateDirectories:YES attributes:nil error:&error]; if (error) NSLog(@"Error creating directory path: %@", [error localizedDescription]); } return SharedDocumentsPath; } - (NSManagedObjectContext*)managedObjectContext { NSManagedObjectContext *ctx = [[[NSManagedObjectContext alloc] init] autorelease]; [ctx setPersistentStoreCoordinator:self.persistentStoreCoordinator]; return ctx; } @end