forked from macrael/correspondance
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved persistence related stuff from the AppDelegate to COPersistence
- Loading branch information
Showing
7 changed files
with
152 additions
and
100 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// | ||
// COPersistence.h | ||
// Correspondence | ||
// | ||
// Created by Marco Paga on 18.05.11. | ||
// Copyright 2011 The Correspondence Team. All rights reserved. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
#import "COConstants.h" | ||
|
||
|
||
@interface COPersistence : NSObject | ||
{ | ||
NSPersistentStoreCoordinator *persistentStoreCoordinator; | ||
NSManagedObjectModel *managedObjectModel; | ||
NSManagedObjectContext *managedObjectContext; | ||
} | ||
|
||
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator; | ||
- (NSManagedObjectModel *)managedObjectModel; | ||
- (NSManagedObjectContext *)managedObjectContext; | ||
|
||
@end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// | ||
// COPersistence.m | ||
// Correspondence | ||
// | ||
// Created by Marco Paga on 18.05.11. | ||
// Copyright 2011 The Correspondence Team. All rights reserved. | ||
// | ||
|
||
#import "COPersistence.h" | ||
|
||
|
||
@implementation COPersistence | ||
|
||
- (id)init | ||
{ | ||
self = [super init]; | ||
if (self) { | ||
// Initialization code here. | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (NSManagedObjectModel *)managedObjectModel { | ||
|
||
if (managedObjectModel != nil) { | ||
return managedObjectModel; | ||
} | ||
|
||
managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain]; | ||
return managedObjectModel; | ||
} | ||
|
||
- (NSPersistentStoreCoordinator *) persistentStoreCoordinator { | ||
|
||
if (persistentStoreCoordinator != nil) { | ||
return persistentStoreCoordinator; | ||
} | ||
|
||
NSFileManager *fileManager; | ||
NSString *applicationSupportFolder = nil; | ||
NSURL *url; | ||
NSError *error; | ||
|
||
fileManager = [NSFileManager defaultManager]; | ||
applicationSupportFolder = [[[NSApplication sharedApplication] delegate] applicationSupportFolder]; | ||
if ( ![fileManager fileExistsAtPath:applicationSupportFolder isDirectory:NULL] ) { | ||
[fileManager createDirectoryAtPath:applicationSupportFolder withIntermediateDirectories:YES attributes:nil error:&error]; | ||
} | ||
|
||
NSString* coreDataFile = [APP_NAME stringByAppendingString:@".sqlite"]; | ||
url = [NSURL fileURLWithPath: [applicationSupportFolder stringByAppendingPathComponent: coreDataFile]]; | ||
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]]; | ||
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]){ | ||
[[NSApplication sharedApplication] presentError:error]; | ||
} | ||
|
||
return persistentStoreCoordinator; | ||
} | ||
|
||
- (NSManagedObjectContext *) managedObjectContext { | ||
|
||
if (managedObjectContext != nil) { | ||
return managedObjectContext; | ||
} | ||
|
||
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; | ||
if (coordinator != nil) { | ||
managedObjectContext = [[NSManagedObjectContext alloc] init]; | ||
[managedObjectContext setPersistentStoreCoordinator: coordinator]; | ||
} | ||
|
||
return managedObjectContext; | ||
} | ||
|
||
- (void)dealloc | ||
{ | ||
[managedObjectContext release]; | ||
managedObjectContext = nil; | ||
|
||
[persistentStoreCoordinator release]; | ||
persistentStoreCoordinator = nil; | ||
|
||
[managedObjectModel release]; | ||
managedObjectModel = nil; | ||
|
||
[super dealloc]; | ||
} | ||
|
||
@end |
This file contains 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