Design Element
Comment on "Lazy Loading of KVO Observed Properties"
by Scott Stevenson — Jun 16
I don't know KVO, but remember that that static bool is static across instances as well

You're right, but it doesn't matter if all of this is being done in a single thread since only one getter is ever being called at a time. The example is simplistic by design. Here's an example for what you suggest (the setter should be the same as above):

@interface MyClass : NSObject { NSImage myImage; BOOL myImageIsBeingSet; } @end @implementation class - (id)init { if ( self = [super init] ) { myImageIsBeingSet = NO; } return self; } - (NSImage*)myImage { if ( myImage == nil && myImageIsBeingSet == NO ) { myImageIsBeingSet = YES; [self setMyImage:[self fetchImageFromDisk]]; myImageIsBeingSet = NO; } return myImage; } @end
Back to "Lazy Loading of KVO Observed Properties"
Design Element

Copyright © Scott Stevenson 2004-2015