Created
January 1, 2010 10:04
-
-
Save haoyayoi/267080 to your computer and use it in GitHub Desktop.
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
// | |
// CoreDateManager.h | |
// | |
// Created by haoyayoi on 09/12/28. | |
// Copyright 2009 haoyayoi. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface CoreDateManager : NSObject { | |
NSManagedObjectModel *managedObjectModel; | |
NSManagedObjectContext *managedObjectContext; | |
NSPersistentStoreCoordinator *persistentStoreCoordinator; | |
} | |
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; | |
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; | |
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator; | |
- (NSString *)applicationDocumentsDirectory; | |
@end | |
@implementation CoreDateManager | |
- (NSManagedObjectModel *)managedObjectModel { | |
if (managedObjectModel != nil) { | |
return managedObjectModel; | |
} | |
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; | |
return managedObjectModel; | |
} | |
- (NSManagedObjectContext *) managedObjectContext { | |
if (managedObjectContext != nil) { | |
return managedObjectContext; | |
} | |
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; | |
if (coordinator != nil) { | |
managedObjectContext = [[NSManagedObjectContext alloc] init]; | |
[managedObjectContext setPersistentStoreCoordinator: coordinator]; | |
} | |
return managedObjectContext; | |
} | |
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { | |
if (persistentStoreCoordinator != nil) { | |
return persistentStoreCoordinator; | |
} | |
NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"***.sqlite"]]; | |
NSError *error = nil; | |
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; | |
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. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button. | |
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. | |
*/ | |
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); | |
abort(); | |
} | |
return persistentStoreCoordinator; | |
} | |
- (NSString *)applicationDocumentsDirectory { | |
return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; | |
} | |
- (void)dealloc { | |
[managedObjectContext release]; | |
[managedObjectModel release]; | |
[persistentStoreCoordinator release]; | |
[super dealloc]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment