Design Element
Comment on "Lazy Loading of KVO Observed Properties"
by mmalc — Jun 19
If the image is set to nil, I'd expect fetchImageFromDisk: to return nil.

The image should be set to nil using a KVO-compliant accessor, and it will therefore emit the appropriate change notification when it is actually changed.

If you really want to invoke a method to set the value in the get accessor, then implement a private set accessor (such as setPrimitiveMyImage:) that both the get and set accessors invoke...

I've seen so, so many bugs that were ultimately traced back to not honoring the KVO contract because the developer thought it wasn't important in that case.

Here it's important that the change notification not be posted -- the value hasn't changed.

(To seriously stretch an analogy, this is almost like measurements in quantum mechanics. By observing the experiment, you're affecting the outcome. If you haven't looked inside the box, you can only assume that the value is correct. As soon as you look, the value is correct and you'll be told if it ever changes again.)

Note also that this pattern is the same as that used by managed objects (see Managed Object Accessor Methods).

Back to "Lazy Loading of KVO Observed Properties"
Design Element

Copyright © Scott Stevenson 2004-2015