0

I have struggled with the setup of KVO for a few hours and I managed to get it to work:

This works:

 [self addObserver:self forKeyPath:@"session.loginState" options:0 context:nil];

This doesn't:

 [self addObserver:self.session forKeyPath:@"loginState" options:0 context:nil];

Please note that self.session lazily creates an empty Session object so self.session is never nil. However, it seems that:

  1. the keypath session.loginState of self is not the same as...
  2. the keypath loginState of self.session from a KVO perspective

Why is this the case?

Besi
  • 22,579
  • 24
  • 131
  • 223

2 Answers2

3

You have your observer and observee backwards. Try

[self.session addObserver:self forKeyPath:@"loginState" options:0 context:nil];
Lily Ballard
  • 182,031
  • 33
  • 381
  • 347
  • Silly mistake, seems like I missed the forrest for the trees, or the selves in my case :-) – Besi Aug 10 '12 at 21:06
1

With this code:

 [self addObserver:self forKeyPath:@"session.loginState" options:0 context:nil];

You are adding self as an observer of self's keypath session.loginState, that is effectively self.session.loginState.

In this code:

 [self addObserver:self.session forKeyPath:@"loginState" options:0 context:nil];

You are adding self.session as an observer of self's keypath loginState, that is effectively self.loginState.

Analog File
  • 5,280
  • 20
  • 23