Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Core data boiler plate
//
// DBController.h
//
// Created by Jesse Crocker on 3/31/13.
// Copyright (c) 2013 Jesse Crocker. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface DBController : NSObject
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
- (NSManagedObjectContext *)managedObjectContext;
- (BOOL)saveMainContext;
- (BOOL)saveContext:(NSManagedObjectContext*)conext;
@end
//
// DBController.m
// WeList
//
// Created by Jesse Crocker on 3/31/13.
// Copyright (c) 2013 Jesse Crocker. All rights reserved.
//
#import "DBController.h"
#import <CoreData/CoreData.h>
#define MODEL_NAME @"WeList"
#define STORE_NAME @"WeList.sqlite"
@interface DBController () {
NSManagedObjectModel *_managedObjectModel;
NSPersistentStoreCoordinator *_persistentStoreCoordinator;
NSManagedObjectContext *_managedObjectContext;
}
@end
@implementation DBController
#pragma mark - public interface
- (BOOL)saveContext:(NSManagedObjectContext*)conext {
NSError *error = nil;
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
NSLog(@"error saving context %@, %@", error, [error userInfo]);
return NO;
}
return YES;
}
- (BOOL)saveMainContext {
return [self saveContext:self.managedObjectContext];
}
#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];
} else {
NSLog(@"Could not get persistent store 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:MODEL_NAME 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 = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:STORE_NAME];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:nil
error:&error]) {
NSLog(@"Error creating persistent store coordinator %@, %@", error, [error userInfo]);
if([[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]) {
[[NSFileManager defaultManager] moveItemAtURL:storeURL
toURL:[[self applicationDocumentsDirectory] URLByAppendingPathComponent:[STORE_NAME stringByAppendingPathExtension:@"backup"]]
error:nil];
return [self persistentStoreCoordinator];
} else {
return nil;
}
}
return _persistentStoreCoordinator;
}
#pragma mark -
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.