Skip to content

Instantly share code, notes, and snippets.

@objectiveSee
Last active August 29, 2015 14:04
Show Gist options
  • Save objectiveSee/22032ee77f32c89e9ea1 to your computer and use it in GitHub Desktop.
Save objectiveSee/22032ee77f32c89e9ea1 to your computer and use it in GitHub Desktop.
I am unable to observe KVO notifications for a derived property (decks) of the EAUser class. The code below never generate a notification when EAUserDecksArray is modified (which modifies decks). I can however make this work if I call will/DidChangeValueForKey: when the dependent property is changed, instead of implementing keyPathsForValuesAffe…
@interface EAUser : NSObject
- (NSArray *)decks;
@end
NSString * const EAUserDecksArray = @"EAUserDecksArray";
- (void)somethingThatChangesDecks:(NSDictionary *)info {
// at some time... decks gets changed like this:
v = [info objectForKey:@"decks"];
if ( v ) {
NSCAssert([v isKindOfClass:[NSArray class]], @"Invalid class.");
[self setValue:v forKey:EAUserDecksArray];
}
};
- (NSArray *)decks
{
id d = [self valueForKey:EAUserDecksArray];
return d;
}
- (NSSet *)keyPathsForValuesAffectingDecks
{
return [NSSet setWithObject:EAUserDecksArray];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
[[EAUser currentUser] addObserver:self forKeyPath:@"decks" options:0 context:0];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@ changed", keyPath); /// <---- this is never called :(
}
@stuartjmoore
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment