gpt4 book ai didi

ios - 实例被释放,而键值观察者仍然在其中注册

转载 作者:技术小花猫 更新时间:2023-10-29 10:09:04 24 4
gpt4 key购买 nike

我有一个 UITableView。

这里我得到了不同的细胞。每个单元格都有一个模型。使用 KVO 和 NotificationCenter,单元会监听模型的变化。当我离开 ViewController 时,出现此错误:

An instance 0x109564200 of class Model was deallocated while key value observers were still registered with it. 
Observation info was leaked, and may even become mistakenly attached to some other object.
Set a breakpoint on NSKVODeallocateBreak to stop here in the debugger. Here's the current observation info:
<NSKeyValueObservationInfo 0x109429cc0> (
<NSKeyValueObservance 0x109429c50: Observer: 0x10942d1c0, Key path: name, Options: <New: NO, Old: NO, Prior: NO> Context: 0x0, Property: 0x10968fa00>
)

在设置/更改模型属性时,我在单元格中执行此操作:

[_model addObserver:self
forKeyPath:@"name"
options:0
context:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(modelIsInvalid:)
name:@"modelIsInvalid"
object:_model];

然后在单元格的dealloc中:

- (void)dealloc
{
NSLog(@"DEALLOC CELL");
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_model removeObserver:self forKeyPath:@"name"];
}

在模型中,我还检查它何时被释放:

- (void)dealloc
{
NSLog(@"DEALLOC MODEL");
}

所有单元格都在所有模型之前被释放,但我仍然收到此错误。另外我不确定如何设置错误中提到的断点。

最佳答案

它不会工作,因为细胞正在被重复使用。因此,当单元格离开屏幕时,它不会被释放,而是进入重用池。

您不应该在单元格中注册通知和 KVO。您应该在 TableView Controller 中执行此操作,当模型更改时您应该更新模型并重新加载可见单元格。

关于ios - 实例被释放,而键值观察者仍然在其中注册,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25056942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com