Last active
August 29, 2015 14:04
-
-
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…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface EAUser : NSObject | |
- (NSArray *)decks; | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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 :( | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if it's not an array? I know KVO is picky with arrays. Try implementing these methods: https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueCoding/Articles/AccessorConventions.html#//apple_ref/doc/uid/20002174-SW4