Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Soulstorm

macrumors 68000
Original poster
Feb 1, 2005
1,887
1
I admit it. I am using singletons. I know what you may all say, and frankly, seeing all these answers on the Internet, saying about the bad aspects of singletons, and advising against them really made me question my programming practices.

I feel I must really clarify some things here and ask fore directions.

So lets consider some cases where I use singletons a lot:

1) To create accessors to global variables, like my root view controller, specific and always existing view controllers, application state, my global managed object context... stuff like that
2) To create utility classes whose job is to handle data application-wide. For example, I create a Singleton that will operate my caching database, which relies on Core Data. Since I need to create caches and other stuff to be put in the database in different views, it somehow felt better to create a class that will handle database ins/outs (being careful about thread safety).
3) Handling network sessions. Actually, I use it to keep alive a connection and sending something like a PINg to a server each XX seconds.

I think that about sums it up. I would really like opinions from other developers on the matter. You think that there are better solutions for these problems above? Do you think that there are always better alternatives to singletons and that they should be avoided?

Any recommendations and thoughts would be useful, and most welcome.
 
Last edited:
Why be concerned over a few singletons -- even if there are more robust/elegant patterns, if it works for you and is maintainable, then go forth and dance, Mr. Bo-singleton.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.