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: Tue, 3 Jul 2007 13:42:45 -0700
On Jul 3, 2007, at 5:57 AM, mmalc Crawford wrote:
With NSPersistentDocument, by default it uses the document's -
fileName as the path for the persistent store. What is the best
way to retarget it so that it uses a different path within the
package? I imagine someone here must have done it, so I'm hoping
to not have to fiddle for so long to figure it out, however it
seems that I might simply be able to override
configurePersistentStoreCoordinatorForURL:ofType:error: and pass a
different URL to super's implementation? Will there be any side
effects to this?
<http://developer.apple.com/documentation/Cocoa/Reference/
ApplicationKit/Classes/NSPersistentDocument_Class/Reference/
Reference.html>
mmalc
I'm not sure how that was supposed to be helpful given that I had
already read the documentation (not to mention the tutorials, sample
code, Google, and the list archives) and came here asking for further
help.
As it turns out, overriding readFromURL:/writeToURL: and
configurePersistentStoreCoordinatorForURL: is working dandily so far.
In my previous experience doing this without Core Data, I had ran
into some trouble with getting this to work, but the circumstances
are different so it's a bit simpler and more direct than I was
expecting it to be.
For posterity, incomplete but demonstrable code of how to do it:
- (BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url ofType:
(NSString *)fileType error:(NSError **)error;
{
NSString * storePath = [[url relativePath]
stringByAppendingPathComponent:@"Data.sqldb"];
return [super configurePersistentStoreCoordinatorForURL:[NSURL
fileURLWithPath:storePath] ofType:fileType error:error];
}
- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName
error:(NSError **)error;
{
NSString * packagePath = [absoluteURL relativePath];
BOOL isDirectory = NO;
if (![[NSFileManager defaultManager]
fileExistsAtPath:packagePath isDirectory:&isDirectory] || !
isDirectory) {
// create an error
return NO;
}
return [super readFromURL:absoluteURL ofType:typeName error:error];
}
- (BOOL)writeToURL:(NSURL *)absoluteURL ofType:(NSString *)typeName
forSaveOperation:(NSSaveOperationType)saveOperation
originalContentsURL:(NSURL *)absoluteOriginalContentsURL error:
(NSError **)error;
{
NSString * packagePath = [absoluteURL relativePath];
BOOL isDirectory = NO;
if (![[NSFileManager defaultManager]
createDirectoryAtPath:packagePath attributes:nil]) {
// create an error
return NO;
}
return [super writeToURL:absoluteURL ofType:typeName
forSaveOperation:saveOperation
originalContentsURL:absoluteOriginalContentsURL error:error];
}
--
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