本文介绍了我应该避免将Singleton用作TabBarController的数据源(以及如何)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TabBarController作为初始场景;每个标签都使用基本相同的信息.此信息仅在运行时可用,并通过json检索.该信息不会由我的应用程序更改,但可能会在另一端更改,并且下次我刷新时可能会更改.

I got a TabBarController as initial scene; every tab uses basically the same information. This information is only available at runtime and is retrieved via json. The information will not be altered by my app but may be altered on the other side and could vary the next time i would refresh it.

这是单身人士的有效案例吗?

Is this a valid case for a singleton?

我曾考虑过在创建TabBarController之前先加载信息并将其传递到其选项卡,但是现在我面临着一些弊端:(每个人前面都有一个很大的AFAIK)

I thought about loading the information before the TabBarController gets created and pass it on to its tabs, but I am confronted with several downsides now: (with a big AFAIK in front of every one)

  • 检索是在绘制UI之前完成的,连接缓慢可能会花费一些时间.
  • 如果没有一百万条通知,那么以后刷新信息是不可能的,因为我无法将其传递回层次结构中.
  • 该应用无法启动/脱机工作,以后无法联机.
  • 它不能与情节提要TabBarController一起使用,因为我必须覆盖所有选项卡的启动.

单例保存信息时,到目前为止,我唯一能看到的缺点是,如果我直接指向后台,则在后台刷新信息可能会使UI显得凌乱.但是我确实可以从每个导航层获得完全的访问权,这确实很诱人.

With a singleton holding the information the only downside i could see so far is that refreshing the information in background could make the UI look messy if i point to it directly. But i would have full access from every navigation tier which is really tempting.

是否还有其他可能的解决方案/最佳实践,如何在TabBarController中共享数据?

Are there other possible solutions/best practices how to share data within a TabBarController?

我希望当我使用单例...:)

I hope this doesn't get closed immediately as i used the word singleton... :)

推荐答案

单例将起作用并且可以被认为是适当的-但不能用作选项卡栏控制器数据源.您正在谈论的应该是单例数据控制器.它可以拥有您的模型数据,并提供触发更新/更改的方法.它可以发布有关更改的通知(不需要知道当前有多少视图控制器对更改感兴趣).

A singleton will work and could be considered appropriate - but not as the tab bar controller data source. What you're talking about should be a singleton data controller. It can own your model data and provide methods for triggering updates / changes. It could post notifications about changes (it doesn't need to know how many view controllers are currently interested in the changes).

这篇关于我应该避免将Singleton用作TabBarController的数据源(以及如何)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 05:15