当控制器被释放时,键值观察者仍然在其中注册

Sud*_*pta 3 memory objective-c key-value-observing dealloc ios

我在代码中添加了一个观察者,然后在dealloc和viewWillDisappear中将其删除但是我仍然收到错误说明

***由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:'类的MyController2的实例0x167e5980被释放,而键值观察者仍然注册了它.

Current observation info: <NSKeyValueObservationInfo 0x16719f90> (
<NSKeyValueObservance 0x16719fb0: Observer: 0x167e5980, Key path: dataContainer.report, Options: <New: YES, Old: YES, Prior: NO> Context: 0x0, Property: 0x1677df30>
)'
Run Code Online (Sandbox Code Playgroud)

我创建了一个控制器,MyController并从中派生出一个新的控制器MyController2.现在我加入了KVO MyController2.

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObserver:self forKeyPath:@"dataContainer.report" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
Run Code Online (Sandbox Code Playgroud)

然后在observeValueForKeyPath中: -

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

    id oldC = [change objectForKey:NSKeyValueChangeOldKey];
    id newC = [change objectForKey:NSKeyValueChangeNewKey];

    if([keyPath isEqualToString:@"dataContainer.report"]) {
        if (oldC != newC) {
            //Remove Observer

            [self removeObserver:self forKeyPath:@"dataContainer.report" context:nil];
            [self updateDataContainer];
            [self reportView];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后我试图在viewWillDisappear和dealloc中删除观察者: -

- (void)dealloc {
    @try{
        [self removeObserver:self forKeyPath:@"dataContainer.report" context:nil];
    }@catch(id anException){
    }
}

-(void) viewWillDisappear:(BOOL)animated{
    @try{
        [self removeObserver:self forKeyPath:@"dataContainer.report" context:nil];
    }@catch(id anException){
    }
    [super viewWillDisappear:animated];
}
Run Code Online (Sandbox Code Playgroud)

我查看丢失的帖子,所有人都说你需要删除观察者.我试图从他们两个中移除观察者,但我仍然遇到了问题.

Sud*_*pta 8

根据我的经验,在Ios中添加和删除观察者的最佳方法.

在ViewDidLoad中添加观察者: -

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addObserver:self forKeyPath:@"dataContainer.report" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
Run Code Online (Sandbox Code Playgroud)

为了观察观察者,我们必须这样做: -

不要删除observeValueForKeyPath中的观察者

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

    id oldC = [change objectForKey:NSKeyValueChangeOldKey];
    id newC = [change objectForKey:NSKeyValueChangeNewKey];

    if([keyPath isEqualToString:@"dataContainer.report"]) {
        if (oldC != newC) {
            [self updateDataContainer];
            [self reportView];
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在dealloc中删除Observer:

这里打电话删除一次

- (void)dealloc {
    @try{
        [self removeObserver:self forKeyPath:@"dataContainer.report" context:nil];
    }@catch(id anException){
    }
}
Run Code Online (Sandbox Code Playgroud)