This is, in fact, an anti-pattern. You should not call -willChangeValueForKey:
followed by -didChangeValueForKey:
without any intervening actual property change. In some cases, doing so can mask KVO problems elsewhere in your code and force observers to update their state related to the property in question. Ultimately, however, you (or the author of the example you cite) should fix the rest of the code so that this anti-pattern is unnecessary.
The correct usage of -will|didChangeValueForKey:
is when you are modifying a property without using KVC-compliant accessors/setters such that the KVO mechanism would not notice the change. For a contrived example, consider modifying the backing instance variable for an attribute directly:
@interface Foo
{
int bar;
}
@end
@implementation Foo
- (void)someMethod
{
bar = 10;
}
@end
KVO observers that had registered for notification of changes in the bar
property would not recieve notification of the change to bar
in -someMethod
. To make the KVO machinery work, you could modify -someMethod
:
- (void)someMethod
{
[self willChangeValueForKey:@"bar"];
bar = 10;
[self didChangeValueForKey:@"bar"];
}
Of course, it would be better to use a @property
declaration and to use KVC-compliant accessors/setters (either manually coded or @synthesized
), but this is a contrived example.