Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

KVO - observeValueForKeyPath not called when observing an enum

I've used NSNotifications before but this is the first time I've tried to use KVO in Cocoa Touch.

My UITableView controller switches between a variety of datasources so I've encapsulated them in different UITableViewDataSource subclasses. I'm trying to have my view controller observe a specific one of these UITableViewDataSource subclasses and track an enum called loadState that reflects the models the load state.

I set the observer like this:

[self.siteUpdatesDataSource addObserver:self
                             forKeyPath:@"loadState"
                                options:0
                                context:nil];

From the debugger I can see that the observer is registered:

(gdb) po [self siteUpdatesDataSource]
<SiteUpdatesTableViewDataSource: 0x651e5a0>
Current language:  auto; currently objective-c
(gdb) po [[self siteUpdatesDataSource] observationInfo]
<NSKeyValueObservationInfo 0x651dd70> (
<NSKeyValueObservance 0x651dd10: Observer: 0xc80f1e0, Key path: loadState, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x651dd90>

)

However, my observeValueForKeyPath method in my viewController never seems to be called. I set a breakpoint and nothing ever reaches it even when I validate that the enum has changed.

- (void) observeValueForKeyPath:(NSString *)keyPath 
                        ofObject:(id)object
                          change:(NSDictionary *)change
                         context:(void *)context {

   [self.tableView reloadData];
}

I appreciate any thoughts on what I'm missing.

like image 954
Nick Avatar asked Mar 11 '11 20:03

Nick


1 Answers

Can't see any problem. Are you sure you change loadState property by calling synthesized accessor or by using KVC or manually (notifying about changes with willChangeValueForKey: and didChangeValueForKey:)?

like image 115
hoha Avatar answered Sep 20 '22 20:09

hoha