j2emanue j2emanue - 11 months ago 102
iOS Question

iOS KVO - Cannot remove an observer

I have a simple Viewcontroller that is KVO compliant and has the following in it:

- (void) viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];

[self addObserver:self forKeyPath:@"importStuff" options:0 context:NULL];
[self addObserver:self forKeyPath:@"importStuffFailed" options:0 context:NULL];

- (void) viewWillDisappear:(BOOL)animated
[super viewWillDisappear:animated];

[self removeObserver:self forKeyPath:@"importStuff"];
[self removeObserver:self forKeyPath:@"importStuffFailed"];

the issue im having is that sometimes user are reporting the following error:

Cannot remove an observer <MyViewController 0x145d0c8d0> for the key path "importStuff" from <MyViewController 0x1741b2280> because it is not registered as an observer.

the addObserver call is not called anywhere else in code. is it something about the life cycles im missing ? isn't viewDidAppear guaranteed to be called once (so it should register the keys right ?)


There's no guarantee that a viewDidAppear will be matched with a viewWillDisappear every time. This means your KVO registration/unregistration would potentially be unbalanced and non-deterministic. You should perform KVO registration/unregistration in guaranteed pairings like viewDidLoad and dealloc.