Should i avoid a singleton as datasource for a TabBarController (and how can i)

社会主义新天地 提交于 2019-12-24 19:28:21

问题


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?

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)

  • The retrieving is done before the UI gets painted, could take some time with slow connectivity.
  • Refreshing the information afterwards is not possible without a million notifications, as i cannot pass it back up in hierarchy.
  • The app cannot start/work offline and switch online later on.
  • It wont work with a storyboard TabBarController because i have to overwrite all tab initiations.

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.

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... :)


回答1:


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).



来源:https://stackoverflow.com/questions/20131094/should-i-avoid-a-singleton-as-datasource-for-a-tabbarcontroller-and-how-can-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!