Design Element
Comment on "Lazy Loading of KVO Observed Properties"
by mmalc — Jun 19
I would personally regard the lazy loading as an implementation detail and not something that should result in a KVO change notification.

Semantically, the value has not changed. It has simply been retrieved. As far as KVO is concerned, before it first asks for the value it is unknown. Therefore:

- (NSImage*)myImage { if ( myImage == nil ) { myImage = [[self fetchImageFromDisk] copy]; } return myImage; }

does not break the KVO contract.

The only unfortunate aspect of this implementation is that it does go against the Cocoa memory management mantra of using accessor methods everywhere. This is one of the cases though when you can break the rule knowingly based on experience.

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

Copyright © Scott Stevenson 2004-2015