KVO 用于具有多个观察者的单例?

发布于 2024-11-17 22:23:42 字数 216 浏览 8 评论 0原文

我目前正在考虑当我的单例属性发生更改时是否使用 KVO 或 NSNotifications 来提醒 iOS 应用程序中的多个对象。

由于所有需要通知更改的对象都引用了单例,因此 KVO 似乎是自然的解决方案。 但是,我从未将 KVO 与单例和多个观察者一起使用,所以我不知道这种模式是否可取甚至可能。

在具有多个观察者的单例上使用 KVO 是一个好主意(甚至可能)吗?

I'm currently contemplating whether to use KVO or NSNotifications to alert several objects in my iOS app when a property of my singleton has changed.

Since all the objects that need to be notified of the change has a reference to the singleton, KVO would seem like the natural solution. However, I've never used KVO with a singleton and multiple observers, so I don't know if that pattern is advisable or even possible.

Is it a good idea (or even possible) to use KVO on a singleton with multiple observers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抠脚大汉 2024-11-24 22:23:42

当您可以等待事件循环时,请使用 NSNotifications。

当您需要立即响应状态变化时,请使用 KVO。

Use NSNotifications when you can wait for the event loop.

Use KVO when you need immediate response to the change in state.

稀香 2024-11-24 22:23:42

使用 KVO 是可行的,因为底层 Cocoa 框架内置了通知观察者的支持,你不需要重新发明轮子

Use KVO in this is feasible, as the underlying Cocoa Framework has support built-in to notify the observers and you need not re-invent the wheel

夜巴黎 2024-11-24 22:23:42

其他答案没有回答真正的问题,即 KVO 是否与多个观察者一起在单例上观察同一事物。

然而,测试表明它工作得很好,而且我还没有看到任何关于这种做法的错误。

The other answers haven't answered the real questions, whether KVO works with multiple observers observing the same thing on a singleton.

Testing, however, shows that it works fine, and I haven't seen anything about the practice being wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文