Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top