Swift-使用NotificationCenter观察价值变化真的不好吗?
只是一个初学者的问题。我希望有人可以帮助我。我正在搜索在Swift中在全球观察变化变化的最佳方法。我遇到了一些文章,说使用NotificationCenter是一个不好的选择,我们应该尽可能避免它。他们建议使用代表和依赖注入。我正在为可重复使用的UI组件使用Delagates。
但是我有一个Websocket连接。我每秒收到此套接字的数据,此数据用于不同的和多个视图控制器(4-6 VC)。现在,我正在使用通知中心,该通知中心收到新数据时通知所有这些VC。
这是正确的方法还是您可以推荐我正确的方法?谢谢。
Just a beginner question. I hope someone can help me. I am searching the best way to observe variable change globally in swift. And I encountered some article that says using NotificationCenter is a bad choice and we should avoid it as much as possible. They are suggesting using delegates and dependency injection. I am using delagates for my reusable UI components.
But I have a websocket connection. I received data from this socket every second, this data are being used in different and multiple view controllers (4-6 VCs). Right now, I am using NotificationCenter that notifies all these VCs when I received new data.
Is this the right approach or can you recommend me the correct way to do this? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据应用程序的部署目标,您可以考虑使用
组合
传播Websocket事件:https://develoveling.apple.apple.apple.com/documentation/combine
Depending on the deployment target of your app, you could consider using
Combine
to propagate websocket events:https://developer.apple.com/documentation/combine