Skip to content

Instantly share code, notes, and snippets.

@ninthspace
Created May 11, 2012 15:38
Show Gist options
  • Save ninthspace/2660504 to your computer and use it in GitHub Desktop.
Save ninthspace/2660504 to your computer and use it in GitHub Desktop.
Class for generating singleton Core Data artifacts
//
// CACoreData.h
// Alisto
//
// Created by Chris Aves on 09/05/2012.
// Copyright (c) 2012 Junctionbox Media. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface CACoreData : NSObject
@property (readonly, strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (readonly, strong, nonatomic) NSManagedObjectModel *managedObjectModel;
@property (readonly, strong, nonatomic) NSPersistentStoreCoordinator *persistentStoreCoordinator;
+ (NSManagedObjectContext *)sharedManagedObjectContext;
+ (NSManagedObjectModel *)sharedManagedObjectModel;
+ (NSPersistentStoreCoordinator *)sharedPersistentStoreCoordinator;
+ (NSURL *)applicationDocumentsDirectory;
+ (void)saveContext;
@end
//
// CACoreData.m
// Alisto
//
// Created by Chris Aves on 09/05/2012.
// Copyright (c) 2012 Junctionbox Media. All rights reserved.
//
#import "CACoreData.h"
@implementation CACoreData
@synthesize managedObjectContext = __managedObjectContext;
@synthesize managedObjectModel = __managedObjectModel;
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator;
+ (NSManagedObjectContext *)sharedManagedObjectContext {
static NSManagedObjectContext *managedObjectContext = nil;
if (!managedObjectContext) {
NSPersistentStoreCoordinator *coordinator = [self sharedPersistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:coordinator];
}
}
return managedObjectContext;
}
+ (NSManagedObjectModel *)sharedManagedObjectModel {
static NSManagedObjectModel *managedObjectModel = nil;
if (!managedObjectModel) {
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Alisto" withExtension:@"momd"];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
}
return managedObjectModel;
}
+ (NSPersistentStoreCoordinator *)sharedPersistentStoreCoordinator {
static NSPersistentStoreCoordinator *persistentStoreCoordinator = nil;
if (!persistentStoreCoordinator) {
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Alisto.sqlite"];
NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self sharedManagedObjectModel]];
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 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;
}
// Returns the URL to the application's Documents directory.
+ (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
+ (void)saveContext
{
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = [self sharedManagedObjectContext];
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();
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment