Theocacao
Leopard
Design Element
Comment on "Lazy Loading of KVO Observed Properties"
by MechaRonzilla — Jun 19
Malcolm's suggestion is the correct one here.
- The getter should call the private setter
- The public setter should call the private setter.

For the case of "what happens when setFoo: is called with a nil argument�", we get this sequence:

1) KVO calls the getter (during your object's willChange�)
2) Enter our public setter which calls our private setter which changes the value (what the new value is is unimportant)
3)�KVO calls the getter (during your object's didChange�) - maybe the result is nil, maybe it's not. That's unimportant.

Now call your getter again. What's the value? The only thing that matters to KVO here is that it's the same as the value that was reported in step 3.

As an aside, I'd argue that the value in step 3 should be non-nil, unless fetchImageBlahBlah returned nil. The trick here is that we depend on fetchImageBlahBlah returning nil for eternity after that.

Now, the only two other places from which you should make calls to the private setter are
- init
- dealloc (assuming you've already removed observers)

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

Copyright © Scott Stevenson 2004-2015