Skip navigation.
 
mlRe: Core Data and Document Packages
FROM : sergio
DATE : Tue Jul 10 20:40:48 2007

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?

Related mailsAuthorDate
mlRe: Core Data and Document Packages sergio Jul 10, 20:40
mlRe: Core Data and Document Packages Seth Willits Jul 10, 22:14
mlRe: Core Data and Document Packages Tim Perrett Jul 11, 14:01
mlRe: Core Data and Document Packages Uli Kusterer Jul 11, 15:16
mlRe: Core Data and Document Packages Tim Perrett Jul 11, 17:03
mlRe: Core Data and Document Packages Seth Willits Jul 11, 21:50