I'm implementing a DocumentsManager
class in iOS, and I want to make a to-many property called documents
to be KVO compliant. It seems to mostly work, and my KVO accessor and mutator methods are called. The thing that bothers me, however, is that any change made directly on the NSMutableArray
proxy returned by calling mutableArrayValueForKey:
on my instance doesn't notify observers.
So, this code notifies me about the insertion of @"aaa"
but not of @"bbb"
, although they are both actually inserted in visible in docsProxy
. Is that expected behavior? If so, what is the advantage of using the mutableArrayValueForKey:
method?
NSMutableArray *docsProxy = [[DocumentsManager instance] mutableArrayValueForKey:@"documents"];
[[DocumentsManager instance] addObserver:self forKeyPath:@"documents" options:NSKeyValueObservingOptionNew context:NULL];
[[DocumentsManager instance] insertObject:@"aaa" inDocumentsAtIndex:0]; // OK
[docsProxy insertObject:@"bbb" atIndex:0]; // no notification!