KVO 和 NSNotifications 的性能速度?
我应该害怕使用键值观察(KVO)和 NSNotifications 吗?我开始在我的应用程序中使用它们,但我对可能触发应用程序范围调用或自动执行操作的概念有点不熟悉,并且有点担心这种开销可能带来的性能影响。
我的担心是没有根据的吗?我应该像使用其他方法调用一样使用它们吗?它们看起来非常方便并且填补了很多空白,所以如果可能的话我想使用它们。注意:我主要为 iOS 设备编程,因此性能始终是我关心的问题。
Should I be afraid of using Key-Value Observations (KVO) and NSNotifications? I'm beginning to use them in my app, but I'm a little unfamiliar with the concept of something that could possibly be triggering an appwide call or automatically doing stuff, and a little afraid of the performance hit that such overhead could bring.
Are my concerns unfounded? Should I use them just like any other method call? They seem very convenient and fill a lot of gaps, so I'd like to use them if possible. Note: I am programming primarily for iOS devices, so performance is always a concern for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Key-Value 观察者和 NSNotifications 都是相当轻量级的,并且在比 iPad 性能更弱的台式机上运行得很好。也就是说,“我应该吗?”的性能问题。类型只能通过分析来回答。我认为您会发现大多数人会建议,在没有任何需要预先设计的特定性能要求的情况下,您以在架构上最有意义且开发效率最高的方式编写应用程序。然后是个人资料,个人资料,个人资料。如果您发现 KVO 或通知占用了应用程序的大量时间(我怀疑情况会如此),那么请考虑更高性能的解决方案(尽管数量不多)。
Both Key-Value observers and NSNotifications are pretty light-weight and were just fine on desktop machines that had less power than an iPad. That said, performance questions of the "should I?" type can only be answered by profiling. I think you'll find most folks would recommend, abscent any specific performance requirements that require up-front design, you write the app in the way that makes most sense architecturally and is most efficient for development. Then profile, profile, profile. If you find that KVO or notifications are taking a significant amount of your apps time (I doubt that will be the case), then consider more performant solutions (though there aren't many).