为什么 KVO 保留所有 NSMutableArray 的对象?
我真的很痛苦地试图弄清楚为什么 KVO 保留了所有正在观察的值。
场景:我有一个带有弱引用的 NSMutableArray。 (使用 CFArrayCreateMutable 进行设置,回调函数的保留和释放都为 NULL)。这意味着添加的任何对象都不会被保留/释放。
我有一个 NSArrayController 观察 NSMutableArray 的值。
但现在,NSMutableArray 的每个对象也被保留。为什么会发生这种情况以及如何阻止这种情况?
I'm having a real pain trying to figure out why KVO is retaining all values that are being observed.
Scenario: I have an NSMutableArray with weak references. (Set up with CFArrayCreateMutable and Callback having NULL for both retain and release). Meaning any object added is never retained / released.
I have an NSArrayController observing values of the NSMutableArray.
But now, every object of the NSMutableArray is also retained. Why is this happening and how do I stop this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你怎么知道有什么东西正在保留这些对象?你没有在看 -retainCount,是吗?如果是,请不要这样做。
如果您了解KVO 的工作方式,您会发现当您启动时,会在幕后创建一些对象观察物体。您确实不需要了解或考虑这些对象才能成功使用 KVO —— 事实上,您能知道的东西并不多 —— 但您也不应该担心除了这些对象之外还有谁你自己可能会也可能不会保留东西。
How do you know that something is retaining these objects? You're not looking at -retainCount, are you? If yes, don't do that.
If you read about the way KVO works, you'll find that there are some objects created behind the scenes when you start observing objects. You really don't need to know or think about those objects in order to use KVO successfully -- indeed, there's not much that you can know -- but you also shouldn't worry about who other than yourself might or might not be retaining things.