Design Element
Comment on "Lazy Loading of KVO Observed Properties"
by Vincent Verville — Jun 17
What I understand is that you want to know if it's possible to send KVO notifications for an ivar (through the "set" accessor) directly from inside the "get" accessor of this ivar.

Since KVO triggers the "get" accessor, you see that simply accessing the ivar will create an infinite loop unless:

a) you add extra state/logic to stop the loop. Or ...

b) you defer the "set" accessor to a third party who will do it after the "get". Or ...

c) Crazy untested idea: you defer the "set" accessor until after the "get" accessor using special ObjC messaging capabilities:
- (NSImage*)myImage { if ( myImage == nil ) { NSImage* theImage = [self fetchImageFromDisk]; [self performSelector: @selector(setMyImage) withObject: theImage afterDelay: 0]; } return myImage; }

d) Another crazy untested idea: you highjack the KVC mechanism to break the cycle by implementing more that one accessor for this ivar. For example, in addition to your special -myImage accessor that usually creates an infinite loop, implement a -getMyImage accessor that does the normal access. All the observers should pull the value returned by -getMyImage due to KVC precedence, and programmatically calling the -myImage accessor should trigger KVO without infinite loop.

e) others possibilities ? ...
Back to "Lazy Loading of KVO Observed Properties"
Design Element

Copyright © Scott Stevenson 2004-2015