How to deal with singleton in iOS
Issue #18
A single singleton
There are many classes that designed to be used as singleton, like UserDefaults.standard
, FileManager.default
, NotificationCenter.default
or even our own classes like UserManager
, Storage
, … Singleton is a design patter and has its own use case, sometimes we still need to use it. But if we are to use singleton, we should just use 1, and group all other singleton under this single singleton. Thanks to Vadym for showing this to me
Swift makes it extremely easy to make singleton, let name it App
then we have a single point of control for all the singletons
1 | struct App { |
AppConfig
This is where we have configurations for staging and production environment, those can be client key, Firebase
configuration, analytics keys, …
Navigator
I use Compass to do central navigation, and there should be 1 Navigator
that does the job
Inject a singleton
Sometime we rely on a singleton to do our job, to make dependencies clear and testing easier, we need to inject this singleton, and leverage Swift default parameter, thanks to John for showing this to me
Here is an example of a ViewModel
that relies on networking
1 | class ProfileViewModel { |