Re: Core Data and Document Packages
Re: Core Data and Document Packages
- Subject: Re: Core Data and Document Packages
- From: sergio <email@hidden>
- Date: Tue, 10 Jul 2007 20:40:48 +0200
Hi all,
this is my first message to the list, and I hope it can be useful to
some of you, particularly to Seth, that raised the initial question...
I have been looking into how making NSPersistenDocument work with
package documents. I ran into the very same problems described in
this thread, but could finally get to a working implementation. Here
it goes (it's ruby, using the rubycocoa bridge, I hope it's ok with
anyone; conversion to Objective C is straightforward, change
underscores in method names to :s, intertwine arguments, add some
memory management...). The very basic idea is overriding the
initialization method, do the url trick there, and everything (except
the recent documents menu, but this could be easily fixed if you need
it) magically works. For me, that's to say... :-)
class MyDocument < OSX::NSPersistentDocument
def initWithContentsOfURL_ofType_error(url, type, err)
url = dataFilePath(url)
ok, err = super_initWithContentsOfURL_ofType_error(url, type, nil)
if (!ok)
# YOUR ERROR MANAGEMENT HERE
end
ok
end
def writeToURL_ofType_forSaveOperation_originalContentsURL_error(url,
type, op, content, error)
if (content == nil)
path = url.relativePath
url = packagePath(url)
if (!
OSX::NSFileManager.defaultManager.createDirectoryAtPath_attributes
(path, nil))
return false
end
end
ok, error =
super_writeToURL_ofType_forSaveOperation_originalContentsURL_error
(url, type, op, content, nil)
if (!ok)
# YOUR ERROR MANAGEMENT HERE
end
ok
end
def readFromURL_ofType_error(url, type, error)
path=url.relativePath
if (!OSX::NSFileManager.defaultManager.fileExistsAtPath_isDirectory
(path, nil))
return false
end
super_readFromURL_ofType_error(url, type, nil)
end
def packagePath(url)
url = OSX::NSURL.fileURLWithPath
(url.relativePath.stringByAppendingPathComponent('data.xml'))
end
end
This is just a skeleton: management of the package directory can be
improved, there is no error management, etc. I guess other solutions
are possible, but this is the only one I could get to work (for me).
You can read more details about this implementation here: <http://
acaro.wordpress.com/2007/07/09/packages-and-core-data-documents/>.
Thank you in advance for any comments or suggestions.
sergio
On Jul 3, 2007, at 3:38 AM, Seth Willits wrote:
> I'm taking the plunge and switching a fair chunk of code to using
> Core Data instead of my own management of an SQLite database. At
> least that's the current plan. ;)
>
> My document-based app has a document type which uses a package
> instead of just a single file to store data in, because there is a
> need to store related files inside the package as well. Currently
> I'm doing some relatively simple overloading of standard NSDocument
> methods to open and save the database by using a path based off of
> the -fileName.
>
> 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?
_______________________________________________
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