5 ms·
As a new Objective-C programmer, I recently ran into the global state problem with an iPhone game I'm working on. I needed some settings that could be accessed/
by Jasber 15y ago
As a new Objective-C programmer, I recently ran into the global state problem with an iPhone game I'm working on. I needed some settings that could be accessed/changed from many different classes. I was told the AppDelegate isn't the right place to put this.
A singleton SettingsManager was suggested, but singletons always get a bad wrap.
So is this a good use of a singleton? What would be the correct way to handle this?
- glassx 15y agoYMMV, but the way I prefer doing it is by telling the settings explicitly to the different classes, instead of having all those classes rely on getting it by themselves from the singleton (this is where the problem lies). You can pass the settings in the constructors, with setter methods or have a "state object" that contains the configuration. For "updating" the states on every class in real time I'd rather rely on explicit state change using the Observer pattern. EDIT: I'd also like to know what other developers do, or if there's anything wrong with my methods, so I second your question.
- mirkules 15y agoI have been wondering the same, about other developers. I am working on an application that periodically pulls information from a server about a physical object (sorry, I can't be more specific than this, so please don't ask). This information needs to be accessed by several different classes, and displayed in view controllers on different tabs. A view controller will pull the information and make it available to all the other view controllers. A singleton provided an effective solution to this because only one data model of my physical object's state can exist at any given time, with the added benefit that I can access it from any view controller without much effort. I may be wrong, but I thought the purpose of a singleton was for models where there can be one and only one instance of your class? Singletons (like other design patterns) were designed to solve a specific problem. But I concede that they are probably overused.
- rbarooah 15y agoLetting individual classes get settings from NSUserDefaults (which itself effectively presents a singleton) directly seems to work pretty well for me. One advantage is that the classes are then reusable in other projects because they aren't coupled to an application specific settings provider.
- hoopadoop 15y agoThe correct way to deal with it is to realise that if you need some settings that can be accessed/changed from many different classes then the design of your app is severely lacking. However, this doesn't necessarily that you need a more complex/sophisticated 'design'. ie. if you stick to TDD this situation is highly unlikely. Hopefully this will stem the tide of questions on Stack Overflow about different Objective-c singleton implementations.
- equark 15y agoThe correct way to deal with this is to realize you have a problem? I think the above comment is willing to admit a problem and is asking for a solution. What does TDD have to do with it? I'm also interested in hearing what is considered the best practice for things like passing around config options, an eventloop, or message bus.
- saraid216 15y agoThe solution is essentially to take a hard look at your design and see why you have to pass around config options, event loops, or message buses to begin with. The need to access global state is a code smell. When you DO need global state, then a singleton object is a reasonable way to achieve it. But chances are fairly high that you don't.
- ebiester 15y agoYou can look for days, and if you don't know any of the alternatives, none will pop out. As I'm not in the mobile sphere, I wouldn't know how to avoid a singleton for an app that is constrained by speed and space requirements. I've already shown an example above, what's your solution in this environment, with the constraints of the questioner?
- Deestan 15y ago> if you need some settings that can be accessed/changed from many different classes then the design of your app is severely lacking. If he's making a game, this is completely acceptable design. Games programming is wildly different from application or server programming. Also, strict TDD is much, much less useful in games programming than anywhere else. Writing unit tests for central data structures and algorithms is useful, of course, but the majority of modules/classes/objects are so small, uncritical, and change completely so often that it's not worth bothering.
- ardit33 15y agohey Jaber. Singletons are fine for that purpose, actually they are probably the best solution. iOS uses singletons everywhere, so feel free to use them. Don't fall the trap of the cargo culture thinking. Google has a completely different problem than you do. Their code spans hundreds of thousands (or millions), of code, in distributed systems, etc.. your iOS app probably has different constrains.
- hoopadoop 15y agoWhere does IOS use Singletons?
- ardit33 15y agoany method that has 'shared' or 'current' in front. http://developer.apple.com/library/ios/#documentation/uikit/reference/UIDevice_Class/Reference/UIDevice.html http://developer.apple.com/library/ios/#documentation/uikit/... It is almost everwhere. [MyClass sharedMyclass] is the Objective-C equivalent of getInstance() Also, singletons make good sense in mobile apps, as it is a very different type of a beast than a server side app.
- mirkules 15y agoSee: http://gigaom.com/apple/iphone-dev-sessions-using-singletons/ http://gigaom.com/apple/iphone-dev-sessions-using-singletons...
- rbarooah 15y agoDepends on your definition of singleton. In java, singletons are enforced by the type system - i.e. There's no public way to make more of the object. Usually that's what people mean by 'singleton' in java. If you don't have the source and you outgrow the single instance, there's nothing you can do except stop using the class. In Objective-C, people do occasionally simulate that by mungng init and alloc, but that is definitely a bad idea. More often a singleton class will have a conventional init and alloc, but have a class method like +sharedInstance which lazily instantiates and dispenses a single instance of the object during the lifetime of the application. That's what people most often mean by 'singleton' in objective-c. It's basically a global variable, but when you outgrow it, it's often not hard to switch to allocating more than one. Also, partly due to what many would see as a deficiency - the fact that it's much harder to produce architecture independent libraries in objective-c than java, the source is available more of the time, so evolving past a singleton is more often straightforward.
- rbarooah 15y agoIf you're using NSUserDefaults, why bother with a SettingsManager? If the SettingsManager is an NSUserDefaults replacement for whatever reason, then it's probably fine for it to be a singleton.
- tszming 15y agoGoogle also use singleton, it really depends on the context and how it's used. e.g. http://code.google.com/mobile/analytics/docs/iphone/ http://code.google.com/mobile/analytics/docs/iphone/