I saw these lines in a demo project, but I couldn’t understand why it did that.
[self willChangeValueForKey:@"names"];
[self didChangeValueForKey:@"names"];
It called didChangeValueForKey immediately after willChangeeValueForKey.
Does it make any sense?
Furthermore, when should be the right time to call this two methods?
Thanks a lot!! 🙂
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:KVO observers that had registered for notification of changes in the
barproperty would not recieve notification of the change tobarin-someMethod. To make the KVO machinery work, you could modify-someMethod:Of course, it would be better to use a
@propertydeclaration and to use KVC-compliant accessors/setters (either manually coded or@synthesized), but this is a contrived example.