When should I call removeObserver: forKeyPath from the closing ViewController class that oversees the persistent model class?

I have a class ViewControllerthat has a property that is a model that I want to observe as properties when the model changes. In my model object, I have a property that is periodically updated against the background of my application. When it is updated, I need to execute the code inside mine ViewController.

To do this, I create an observer on my model from my method ViewController viewDidLoad.

[ModelObject addObserver:self 
              forKeyPath:@"State" 
                 options:NSKeyValueObservingOptionNew 
                 context:nil];

As you can see, this is nothing special, and the observation method behaves as it should while I leave the view displayed on my screen. If I remove the above view from the parent view, I get an error EXC_BAD_ACCESSwhen changing the ModelObjectinstance property Mode. In particular, my application crashes in the line that updates the property Mode, and I get mostly useless EXC_BAD_ACCESSin the next line of code in the ModelObject instance.

//This is located in a method that periodically toggles the value of "State"
[self setState: 2];

, [ModelObject removeObserver: self forKeyPath:@"State"] - ViewController, subview. , viewDidUnload, , viewDidUnload . , , - .

? , KVO, , , . , , . , , , subview?

+5
2

addObserver: removeObserver: viewWillAppear: viewWillDisappear:. , , viewDidLoad viewDidUnload.

+9

-dealloc

+1

All Articles