KVO 观察与 NSNotificationCenter 观察
我想知道在 KVO 与 NSNotificationCenter 观察中是否有理由使用其中之一。性能、内存使用、速度等?
I'm wondering if there is a reason to use one over the other in KVO vs NSNotificationCenter observations. Performance, memory usage, speed, etc?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两者并不总是可以互换的。从概念上讲,KVO 仅用于观察对象的属性。例如,您不能使用 KVO 来替换 NSApplicationWillTerminateNotification,因为它通知观察者事件发生,而不是对象属性的更改。
至于性能和内存使用,它们都很快并且使用的内存可以忽略不计。
NSNotificationQueue
具有合并功能以阻止通知泛滥。据我所知,KVO 没有任何合并,这确实一度给我带来了性能问题。我观察了数百个对象,当这些对象发生批量更新时,我会收到数百个 KVO 回调。这不是 KVO 本身的性能问题,而是我自己的代码由于批量更新而运行的问题。性能并不是真正的问题,更多的是最适合问题的问题。如果是属性改变,就使用KVO。如果不是属性更改,请使用委托或通知,具体取决于您需要单个观察者还是多个观察者。
The two are not always interchangeable. Conceptually, KVO is only for observing a property of an object. For example, you can't use KVO to replace
NSApplicationWillTerminateNotification
because it notifies observers about an event happening, not a change in a property of an object.As for performance and memory usage, they are both fast and use negligible memory.
NSNotificationQueue
has coalescing to stop floods of notifications. KVO doesn't have any coalescing as far as I know, which did cause performance issues for me at one point. I was observing hundreds of objects, and when a batch update happened to those objects I'd get hundreds of KVO callbacks. It wasn't a performance issue with KVO itself, but with my own code running as a result of the batch update.Performance isn't really the issue, it's more about the best fit for the problem. If it's a property change, use KVO. If it's not a property change, use a delegate or a notification depending on whether you need a single observer or multiple observers.
这是一个很老的问题,但想添加一些要点。我同意 Tom Dalling 的回答,但是,在大型应用程序中有很多场景,我们倾向于为以下属性添加观察者:一个对象,我们不能,或者,我们错过了将它们从观察者列表中删除的情况。
让我们考虑我的应用程序中的以下场景 - ViewController 显示一个蛇对象,我正在观察该对象的属性更改 - “毒液”。因此,每当 viewController 需要显示不同的蛇时,我只需从该蛇对象的观察者中删除视图控制器即可。
该应用程序演变为显示蛇列表而不是单条蛇,这意味着我必须观察该对象中所有蛇的属性。现在,当从数组中删除一条旧蛇时,我应该了解此事件,以便我可以从该蛇对象中删除作为观察者的视图控制器。为此,我必须首先观察阵列本身的变化。为此,我必须遵循特定协议将对象插入数组并将它们从数组中删除。这样,复杂性就建立起来了。我们都知道不从对象中删除观察者以及该对象是否被操作系统释放的后果!
上面只是一个例子,这里的主要问题是我无法获取给定对象的KVO观察者列表,以便在该对象释放之前将它们从观察者中删除 - 这可以通过 NSNotification 轻松实现和 NSNotificationCenter。有时,我倾向于使用 NSNotification 而不是 KVO,但是,就良好的设计实践而言,KVO 始终优于通知。
A very old question, but thought of adding some points. I agree with Tom Dalling's answer, however, there are many scenarios in big applications where we tend to add observer for a property of an object and we cannot, or, we miss out removing them from list of observers.
Let us consider the following scenario from my application - A ViewController displays a snake object, I am observing for a property change on this object - "venom". So whenever viewController needed to show a different snake I would simply remove the view controller from observer of that snake object.
The app evolved to show a list of snakes instead of a single snake, this means I had to observe for property of all snakes in that object. Now, when an old snake is removed from the array I should get to know about this event so that I can remove view controller as observer from this snake object. To do this, I have to first observe for changes on the array itself. To do this I have to follow particular protocol to insert objects into array and remove them from array. This way the complexity builds on. We all do know the consequences of not removing observer from an object and if that object is released by the OS!
Above is just one example to cite, the main problem here is I cannot get the list of KVO observers for a given object to remove them from observers before this object gets released - This can be easily achieved by NSNotification and NSNotificationCenter. At times, I tend to be inclined towards using NSNotification over KVO, however, KVO always has edge over notification in terms of good design practice.