Created
June 8, 2012 21:44
-
-
Save flipjorge/2898246 to your computer and use it in GitHub Desktop.
iOS Core Data Stack snippet
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 characters
#import <Foundation/Foundation.h> | |
@interface CoreDataStack : NSObject | |
@property (readonly, nonatomic) NSManagedObjectContext *managedObjectContext; | |
@property (readonly, nonatomic) NSManagedObjectModel *managedObjectModel; | |
@property (readonly, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator; | |
@property (nonatomic, copy) NSString *dataModelFilename; | |
@property (nonatomic, copy) NSString *storePath; | |
@property (nonatomic, copy) NSString *storeType; | |
+(CoreDataStack*)shared; | |
-(void)saveContext:(NSManagedObjectContext*)context; | |
@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 characters
#import "CoreData/CoreData.h" | |
#import "CoreDataStack.h" | |
#import "Directory.h" | |
@implementation CoreDataStack | |
#pragma mark singleton implementation | |
static CoreDataStack* _shared = nil; | |
+(CoreDataStack*)shared{ | |
@synchronized([CoreDataStack class]) | |
{ | |
if(!_shared) | |
_shared = [[self alloc] init]; | |
return _shared; | |
} | |
return nil; | |
} | |
+(id)alloc | |
{ | |
@synchronized([CoreDataStack class]) | |
{ | |
NSAssert(_shared == nil, @"Attempted to allocate a second instance of the CoreDataStack singleton"); | |
_shared = [super alloc]; | |
return _shared; | |
} | |
return nil; | |
} | |
#pragma mark - public methods | |
@synthesize managedObjectContext = _managedObjectContext; | |
@synthesize managedObjectModel = _managedObjectModel; | |
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator; | |
@synthesize dataModelFilename = _dataModelFilename; | |
@synthesize storePath = _storePath; | |
@synthesize storeType = _storeType; | |
- (void)saveContext:(NSManagedObjectContext*)context | |
{ | |
NSError *error = nil; | |
NSManagedObjectContext *managedObjectContext = (context == nil) ? self.managedObjectContext : context; | |
if (managedObjectContext != nil) | |
{ | |
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) | |
{ | |
/* | |
Replace this implementation with code to handle the error appropriately. | |
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
*/ | |
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); | |
abort(); | |
} | |
} | |
} | |
#pragma mark - Core Data stack | |
/** | |
Returns the managed object context for the application. | |
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. | |
*/ | |
- (NSManagedObjectContext *)managedObjectContext | |
{ | |
if (_managedObjectContext != nil) | |
{ | |
return _managedObjectContext; | |
} | |
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; | |
if (coordinator != nil) | |
{ | |
_managedObjectContext = [[NSManagedObjectContext alloc] init]; | |
[_managedObjectContext setPersistentStoreCoordinator:coordinator]; | |
} | |
return _managedObjectContext; | |
} | |
/** | |
Returns the managed object model for the application. | |
If the model doesn't already exist, it is created from the application's model. | |
*/ | |
- (NSManagedObjectModel *)managedObjectModel | |
{ | |
if (_managedObjectModel != nil) | |
{ | |
return _managedObjectModel; | |
} | |
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:self.dataModelFilename withExtension:@"momd"]; | |
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; | |
return _managedObjectModel; | |
} | |
/** | |
Returns the persistent store coordinator for the application. | |
If the coordinator doesn't already exist, it is created and the application's store added to it. | |
*/ | |
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator | |
{ | |
if (_persistentStoreCoordinator != nil) | |
{ | |
return _persistentStoreCoordinator; | |
} | |
NSURL *storeURL = [NSURL fileURLWithPath:[self storePath]]; | |
NSError *error = nil; | |
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; | |
//NSSQLiteStoreType | |
//NSInMemoryStoreType | |
if(!_storeType) | |
_storeType = NSInMemoryStoreType; | |
if (![_persistentStoreCoordinator addPersistentStoreWithType:_storeType | |
configuration:nil URL:storeURL options:nil | |
error:&error]) | |
{ | |
/* | |
Replace this implementation with code to handle the error appropriately. | |
abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. | |
Typical reasons for an error here include: | |
* The persistent store is not accessible; | |
* The schema for the persistent store is incompatible with current managed object model. | |
Check the error message to determine what the actual problem was. | |
If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. | |
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]; | |
Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. | |
*/ | |
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); | |
abort(); | |
} | |
return _persistentStoreCoordinator; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment