Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6994753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:55:55+00:00 2026-05-27T19:55:55+00:00

I commonly create apps that require storage of data, and this data is used

  • 0

I commonly create apps that require storage of data, and this data is used across the entire program. It’s not much, though, so I usually use NSUserDefaults to load/save this data. However, the saving/loading code, along with packing/unpacking the data, takes up space, and I thought moving this code to reusable methods inside a global singleton would be a good idea. It seems to have worked well.

Even so, I’ve read a lot lately on the evils of singletons and global objects, and I’ve started to have second thoughts. People often say that the use of singletons is almost always an indications of poor design. For the most part, I’d disagree (I think simple uses like this are a good design pattern), but I’m certainly no expert on the matter.

So, is using singletons even in a simple way like this bad? If so, what’s the better alternative?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:55:56+00:00Added an answer on May 27, 2026 at 7:55 pm

    I definitely don’t agree that singletons are evil. They are sometimes overused perhaps but on some occasions are just perfect for the job. In some applications it makes sense to have some kind of general data manager. The singleton pattern is used extensively in the SDK itself (app delegates, shared managers, default centers and so on). Most often these are not “pure” singletons, as in you can access a shared instance but can also create new instances if necessary.

    The question you need to ask yourself is whether it would be useful to have access to a single instance of a data manager from anywhere at anytime, if it isn’t then a singleton is probably not needed. If you are going to be using singletons in multi-threaded environments however, you do need to worry about race conditions (can one thread modify a resource while another is accessing it), the documentation has good explanations on how best to achieve this in Cocoa.

    Let me try to explain with an example – Am using some code from a game I wrote. Let’s say you have a GameMap class and a Tile class. The GameMap represents a 2 dimension grid of Tile objects.

    GameMap *gameMap = [[GameMap alloc] init];
    NSArray *theTiles = gameMap.tiles;
    

    The instance of the GameMap owns the grid of tiles, and creates the tiles when the game map is created. No singleton needed.

    You may say “but I only have one GameMap at a time, what’s the big deal?”. What about loading saved games, or loading new levels? Well that becomes as easy as:

    // In whatever class object owns the game map
    self.gameMap = [[GameMap alloc] initWithSaveData:saveData];
    

    In conclusion, create an instance of a class that has code to manage other instances of things. Keep as little global as possible and your code will be more scalable and maintainable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a UserSiteBaseController that gets commonly used data and sets the data
I am reading about COFF file formats, which is commonly used to create an
I'm trying to create shortcut keys for some commonly used sudo shell commands (for
To quote Wikipedia : Two commonly used languages that support many kinds of implicit
I know it is commonly used as a lock object, but is that really
I intend to create a java program/service that continuously polls rss-feeds using the informa
I want to create a factory that will create commonly mocked objects for my
I am planning to create a Licensing Module which can be commonly used for
I want to create a function that compares a password against some commonly idiotic
I want to create a method in superclass of test-class that stubs some commonly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.