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!
It turns out that mutableArrayValueForKey:
does not always return a notifying array. It only does so when observers have already been registered on the observed object!
So swapping my first two lines fixes the problem:
[[DocumentsManager instance] addObserver:self forKeyPath:@"documents" options:NSKeyValueObservingOptionNew context:NULL];
NSMutableArray *docsProxy = [[DocumentsManager instance] mutableArrayValueForKey:@"documents"];
Can't help thinking how much time we'd save if we could read the source code of those methods…
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With