Core data, array controller, and add:

  • Hello,

    I have a data model that consists of Parent, which has a to-many
    relationship with a Child, which has the inverse relationship to Parent.

    Each of these objects, Parent and Child, have associated
    NSArrayControllers.  These controllers are used to set up a typical
    Master-Detail relationship.  This displays the model data correctly,
    and is able to add:/remove: objects to/from the managed object context.

    I now want to intercept the point where the newly created Child is
    added to its Parent, as I need to update a non-standard attribute
    that maintains ordering.  I had thought to perform this in the Parent
    relationship methods (e.g. addChildObject:)

    However, this method is not being called by the default
    NSArrayController add: method.  Do I need to create a custom
    NSArrayController to ensure that addChildObject: is called on the
    Parent?  Or am I missing something basic here?

    Thanks,  Dale
  • On Nov 1, 2006, at 1:51 PM, Dale Schack wrote:

    > I now want to intercept the point where the newly created Child is
    > added to its Parent, as I need to update a non-standard attribute
    > that maintains ordering.  I had thought to perform this in the
    > Parent relationship methods (e.g. addChildObject:)
    > However, this method is not being called by the default
    > NSArrayController add: method.  Do I need to create a custom
    > NSArrayController to ensure that addChildObject: is called on the
    > Parent?  Or am I missing something basic here?
    >
    This is (unfortunately) due to a (known) bug in NSArrayController; it
    does not invoke custom accessor methods.

    You can work around this if you can introduce '.self' into the
    keypath, for example:

    If the contentSet of your detail array controller is bound to
    [MasterArrayController].selection.children

    then change it to
    [MasterArrayController].selection.self.children

    mmalc
  • mmalc,

    Works like a charm.  Thanks.

    As an aside, I see that this is covered in the Core Data FAQ.  My
    apologies for not having read the FAQ prior to posting the question.

    Dale

    On 1-Nov-06, at 9:01 PM, mmalc crawford wrote:

    >
    > On Nov 1, 2006, at 1:51 PM, Dale Schack wrote:
    >
    >> I now want to intercept the point where the newly created Child is
    >> added to its Parent, as I need to update a non-standard attribute
    >> that maintains ordering.  I had thought to perform this in the
    >> Parent relationship methods (e.g. addChildObject:)
    >> However, this method is not being called by the default
    >> NSArrayController add: method.  Do I need to create a custom
    >> NSArrayController to ensure that addChildObject: is called on the
    >> Parent?  Or am I missing something basic here?
    >>
    > This is (unfortunately) due to a (known) bug in NSArrayController;
    > it does not invoke custom accessor methods.
    >
    > You can work around this if you can introduce '.self' into the
    > keypath, for example:
    >
    > If the contentSet of your detail array controller is bound to
    > [MasterArrayController].selection.children
    >
    > then change it to
    > [MasterArrayController].selection.self.children
    >
    > mmalc
    >
previous month november 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      
Go to today