Re: Core Data and Document Packages
Re: Core Data and Document Packages
- Subject: Re: Core Data and Document Packages
- From: Seth Willits <email@hidden>
- Date: Wed, 11 Jul 2007 12:50:18 -0700
On Jul 11, 2007, at 5:01 AM, Tim Perrett wrote:
This is a very interesting thread...
I would agree with seth that you dont want it to appear to the user
what is inside the bundle, I too am trying to achive somthing like
this and would be interested in putting something rough together as
an example implementation. Sergio has started it in RubyCocoa, so
it would probably not take much to get a more robust working sample
together.
Well if you don't want it to appear to the user to be inside the
bundle, then you don't want to use Sergio's method. :) I have simple
code that works fine, but I haven't had time to play with Save As,
Save a copy, or autosave, etc to see what happens there. Also, ignore
the improperness of me using Cocoa error 0. I just wanted to put
something in there so it wouldn't crash if it failed ;)
@implementation MyDocument
- (id)init
{
if (!(self = [super init])) {
return nil;
}
return self;
}
- (void)dealloc;
{
[managedObjectModel release];
[managedObjectContext release];
[persistentStoreCoordinator release];
[super dealloc];
}
- (NSString *)windowNibName
{
return @"MyDocument";
}
- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];
}
#pragma mark -
- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)
absoluteURL ofType:(NSString *)fileType error:(NSError **)error;
{
NSString * packagePath = [absoluteURL relativePath];
NSString * storePath = [packagePath
stringByAppendingPathComponent:@"ProjectStore.sqldb"];
id store = nil;
store = [[[self managedObjectContext] persistentStoreCoordinator]
addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:[NSURL fileURLWithPath:storePath]
options:nil error:error];
if (store)
hasConfiguredPersistentStoreCoordinator = YES;
return (store != nil);
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator;
{
if (!persistentStoreCoordinator) {
persistentStoreCoordinator = [[NSPersistentStoreCoordinator
alloc] initWithManagedObjectModel:[self managedObjectModel]];
}
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;
}
- (id)managedObjectModel;
{
if (!managedObjectModel) {
NSBundle * bundle = [NSBundle bundleForClass:[self class]];
NSString * path = [bundle pathForResource:@"MyDocument"
ofType:@"mom"];
NSURL * url = [NSURL fileURLWithPath:path];
managedObjectModel = [[NSManagedObjectModel alloc]
initWithContentsOfURL:url];
}
return managedObjectModel;
}
#pragma mark -
#pragma mark Load Save
- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName
error:(NSError **)error;
{
NSFileManager * fm = [NSFileManager defaultManager];
NSString * packagePath = [absoluteURL relativePath];
BOOL isDirectory = NO;
if (![fm fileExistsAtPath:packagePath isDirectory:&isDirectory]
|| !isDirectory) {
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:0
userInfo:nil];
return NO;
}
if (![self configurePersistentStoreCoordinatorForURL:absoluteURL
ofType:typeName error:error]) {
return NO;
}
return YES;
}
- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName
forSaveOperation:(NSSaveOperationType)saveOperation
originalContentsURL:(NSURL *)absoluteOriginalContentsURL error:
(NSError **)error;
{
NSFileManager * fm = [NSFileManager defaultManager];
NSString * packagePath = [absoluteURL relativePath];
BOOL isDirectory = NO;
if ([fm fileExistsAtPath:packagePath isDirectory:&isDirectory]) {
if (!isDirectory) {
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:0
userInfo:nil];
return NO;
}
} else if (![fm createDirectoryAtPath:packagePath attributes:nil]) {
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:0
userInfo:nil];
return NO;
}
if (!hasConfiguredPersistentStoreCoordinator) {
if (![self
configurePersistentStoreCoordinatorForURL:absoluteURL ofType:typeName
error:error]) {
return NO;
}
}
if ([self managedObjectContext]) {
if ([[self managedObjectContext] commitEditing]) {
if ([[self managedObjectContext] hasChanges] && ![[self
managedObjectContext] save:error]) {
return NO;
}
} else {
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:0
userInfo:nil];
return NO;
}
}
return YES;
}
@end
--
Seth Willits
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden