Source list tree nodes able to represent different entities

  • Hi,

    I'm working on a core data application that has a UI similar to iTunes:

    - NSOutlineView as source list with groups on the left
    - NSTableView to display the actual content on the right

    Just as in iTunes I now want to display properties of different object
    classes in that NSTableView, depending on what the user selects in the
    source list tree. When you select "Podcasts" in iTunes' source list
    you are obviously presented with instances of the "podcast" class and
    not the "song" class.

    Example:

    BOOKS-GROUP
        Book A            ->  Chapters of Book A shown in table
        Book B            ->  Chapters of Book B shown in table
        Book C            ->  Chapters of Book C shown in table

    MAGAZINES-GROUP
        Magazine 1    ->  Articles of Magazine 1 shown in table
        Magazine 2    ->  Articles of Magazine 2 shown in table
        Magazine 3    ->  Articles of Magazine 3 shown in table

    I started designing my data model by creating a TreeNode entity that
    can be used to form my entire source list tree. These have a
    "representedObject" to-one relationship to point to the actual
    container object (book or magazine) that contains the items (chapters
    or articles) shown in the table.

    But I don't know how to work around the fact that a relationship like
    my TreeNodes "representedObject" wants a single entity it relates to.
    I want it to relate to more than one entity!

    Unfortunately, the usual example-suspects (CoreRecipies, SourceList
    Sample) seem to work with only one entity/class.

    So does anyone know how to implement a source list tree that points to
    objects of different entities? Any help is greatly appreciated!! Thanks.

    Sebastian
  • On Dec 30, 2007, at 4:51 PM, Sebastian Morsch wrote:

    > But I don't know how to work around the fact that a relationship
    > like my TreeNodes "representedObject" wants a single entity it
    > relates to. I want it to relate to more than one entity!
    >
    First, do you need to use a TreeNotes entity?  Can you not just use
    the data entities directly.

    Note:
    "An NSTreeController requires that you describe how the tree of
    objects is traversed by specifying the key path for child objects. All
    child objects for the tree must be key-value-coding compliant for the
    same child key path. If necessary you should implement accessor
    methods in your model classes, or categories on those classes, that
    map the child key to the appropriate class-specific method name."

    <http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/NSTreeController_Class/Reference/Reference.html
    >

    > Unfortunately, the usual example-suspects (CoreRecipies, SourceList
    > Sample) seem to work with only one entity/class.
    > So does anyone know how to implement a source list tree that points
    > to objects of different entities? Any help is greatly appreciated!!
    > Thanks.
    >
    Use entity inheritance.  Define an entity (perhaps abstract) that
    defines the relationship, then implement the other entities as sub-
    entities.

    mmalc
  • Thank you for the quick answer mmalc!

    You're right, a "TreeNode" entity is probably not useful for concrete
    objects that only represent other data entities. I will rather try
    using it as an abstract superclass for my data entities to let them
    inherit the attributes NSTreeController expects.

    NSTreeController will then be assigned to the "TreeNode" entity rather
    than its subclasses (the data entities) for the sake of flexibility.

    I only will have to insert new objects directly into the managed
    object context myself and not with NSTreeController's newObject method
    because it would only create TreeNode-objects.

    I hope I got it right now, thanks again!

    Am 31.12.2007 um 02:34 schrieb mmalc crawford:

    >
    > On Dec 30, 2007, at 4:51 PM, Sebastian Morsch wrote:
    >
    >> But I don't know how to work around the fact that a relationship
    >> like my TreeNodes "representedObject" wants a single entity it
    >> relates to. I want it to relate to more than one entity!
    >>
    > First, do you need to use a TreeNotes entity?  Can you not just use
    > the data entities directly.
    >
    > Note:
    > "An NSTreeController requires that you describe how the tree of
    > objects is traversed by specifying the key path for child objects.
    > All child objects for the tree must be key-value-coding compliant
    > for the same child key path. If necessary you should implement
    > accessor methods in your model classes, or categories on those
    > classes, that map the child key to the appropriate class-specific
    > method name."
    >
    > <http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Cla
    sses/NSTreeController_Class/Reference/Reference.html
    > >
    >
    >
    >> Unfortunately, the usual example-suspects (CoreRecipies, SourceList
    >> Sample) seem to work with only one entity/class.
    >> So does anyone know how to implement a source list tree that points
    >> to objects of different entities? Any help is greatly appreciated!!
    >> Thanks.
    >>
    > Use entity inheritance.  Define an entity (perhaps abstract) that
    > defines the relationship, then implement the other entities as sub-
    > entities.
    >
    > mmalc
previous month december 2007 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