CoreData, NSDocument and NSApplication

  • Hi all,

    ok, so Im rather new to the CoreData stuff and Im trying to retrofit it
    onto an existing application.

    The existing application is a multiple document application that opens
    and displays various types of files using the very boring, very cool
    framework that is provided by cocoa.

    Ive created a datastore file, and added some entities and some
    attributes.

    Ive added a NSArrayController to the document nib and bound it to the
    file owner.

    If I change the super of my document subclass to NSPersistentDocument
    then everything appears to work honkydory.

    the problem is that what I _want_ is to have a single datastore that
    every document accesses, and not a separate one for every document.

    so how can I set things up so that every document is accessing the same
    central datastore?  I tried binding the NSArrayController to the
    sharedApplication, but that just throws an error.

    whats the best way to do this?

    Yours cheerfully,

    Andrew Bush
  • Andrew,

    I've thought about this too but I haven't tried it. I think it has to
    do with having all the editing context's use the same
    NSPersistentStoreCoordinator.

    If you look at a non-Document based CoreData app, the application
    delegate sets up CoreData but in a Document-based app, the setup is
    built-in to the NSPersistantDocument (which your MyDocument
    subclasses). I was thinking that you can override -
    managedObjectContext to return the Application's global
    NSManagedObjectContext which uses the global
    NSPersistentStoreCoordinator. Possibly, you would use a different
    object context but the same store.

    Just a thought. I haven't tried it but it seems like it would work. I
    don't know if there are issues related to having multiple object
    contexts for the same store. I'm also not sure about using the same
    object context for multiple documents - I think that has potential
    for more problems. It seems like you'd want your own object context.

    Hope this helps
    Marc

    On Oct 11, 2006, at 8:46 AM, Andrew Bush wrote:

    > Hi all,
    >
    > ok, so Im rather new to the CoreData stuff and Im trying to
    > retrofit it onto an existing application.
    >
    > The existing application is a multiple document application that
    > opens and displays various types of files using the very boring,
    > very cool framework that is provided by cocoa.
    >
    > Ive created a datastore file, and added some entities and some
    > attributes.
    >
    > Ive added a NSArrayController to the document nib and bound it to
    > the file owner.
    >
    > If I change the super of my document subclass to
    > NSPersistentDocument then everything appears to work honkydory.
    >
    > the problem is that what I _want_ is to have a single datastore
    > that every document accesses, and not a separate one for every
    > document.
    >
    > so how can I set things up so that every document is accessing the
    > same central datastore?  I tried binding the NSArrayController to
    > the sharedApplication, but that just throws an error.
    >
    > whats the best way to do this?
    >
    >
    > Yours cheerfully,
    >
    >
    > Andrew Bush
    >
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/<marcrespass...>
    >
    > This email sent to <marcrespass...>
  • Marc is on the right track.  If you want to have a document-based
    application where each document actually represents data from a shared
    persistent store, one way to do it would be to have each document's
    NSManagedObjectContext share a single NSPersistentStoreCoordinator.

    If you want each of your documents to actually represent a query
    that's archived to a file or something along those lines, I'd use
    NSDocument instead of NSPersistentDocument, to avoid having to break
    any assumptions the latter might be making.  NSPersistentDocument
    expects the file that it represents on disk to be a persistent store;
    rather than trying to subvert that assumption, I'd just write the
    couple lines of code to give each of my documents its own
    NSManagedObjectContext and have it use the documents NSUndoManager.

      -- Chris
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today