Propagation of KVO through bindings

  • Hi,

    If I set up key-value observing of an NSArrayController's
    arrangedObjects like so:

    [myArrayController addObserver:self forKeyPath:@"arrangedObjects"
    options:(NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew)
    context:nil];

    I get informed when the array controller's content changes.

    Now if I set up an extra level of indirection:

    myDictionary = [[NSMutableDictionary alloc] init];
    [myDictionary bind:@"boundArrangedObjects" toObject:myArrayController
    withKeyPath:@"arrangedObjects" options:nil];
    [myDictionary addObserver:self forKeyPath:@"boundArrangedObjects"
    options:(NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew)
    context:nil];

    I would expect to be informed of the change twice, but though (if I
    log its contents) myDictionary.array seems to be being kept up to
    date, KVO is not informing me of the change through the binding.

    Is this expected behaviour, and if so, is it possible to get the
    notifications I want some other way?

    Thanks,
    Hamish

    P.S. I have tried the array controller in both entity and class modes
    and it seems to make no difference.
  • On Wed, Apr 16, 2008 at 4:49 PM, Hamish Allan <hamish...> wrote:

    > [myArrayController addObserver:self forKeyPath:@"arrangedObjects"
    > options:(NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew)
    > context:nil];
    > myDictionary = [[NSMutableDictionary alloc] init];
    > [myDictionary bind:@"boundArrangedObjects" toObject:myArrayController
    > withKeyPath:@"arrangedObjects" options:nil];
    > [myDictionary addObserver:self forKeyPath:@"boundArrangedObjects"
    > options:(NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew)
    > context:nil];

    > [...]
    > KVO is not informing me of the change through the binding.

    For anyone reading the archives, I have done some more digging and
    found out what the problem is. It appears that NSArrayController does
    not send proper KVO notifications for arrangedObjects -- whatever the
    change, it reports the old and new values to be nil, which causes the
    default implementation of bind:toObject:withKeyPath:options: to drop
    the notification rather than forward it (as the old and new values are
    equal). I would encourage anyone who uses NSArrayController to file a
    bug (mine is rdar://problem/5870167).

    Best wishes,
    Hamish
previous month april 2008 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
MindNode
MindNode offered a free license !