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 9321999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:58:50+00:00 2026-06-19T03:58:50+00:00

I use a sinlgeton in my application for managing data that is available to

  • 0

I use a sinlgeton in my application for managing data that is available to the whole application, which accessed via:

static MMProductManager *sharedInstance = nil;
+(MMProductManager*)SharedInstance {
    dispatch_once( &resultsToken, ^(void) {
        if ( ! sharedInstance ) {
            sharedInstance = [[MMProductManager alloc] init];
        }
    });
    return sharedInstance;
}

Everything is working as expected.

In Objective C, there does not seem to be a way to hide any object’s init method, and in my case having more than instance of MMProductManager would lead to data being duplicated (in the best case scenario).

What I would like to do is guard against instantiating more than one instance. Other languages seem to have this feature; i.e. marking certain methods/classes as private. I am thinking of implementing something along like:

-(id)init {
    // guard against instantiating a more than one instance
    if ( sharedInstance )
        return sharedInstance;

    if ( (self = [super init]) ) {
        self->_resultsQueue = dispatch_queue_create( kMMResultQLAbel, NULL );
        self->_initialized  = FALSE;

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(handleNotification:)
                                                     name:UIApplicationDidReceiveMemoryWarningNotification
                                                   object:0];

        [self initialize];
    }

    return self;
}

Does this approach seem reasonable?

What would happen in the case of someone allocating this class, then calling the init described above? Would it be reasonable to override +(id)alloc? If so How would I go about doing that?

I know the convention of exposing a SharedInstance method is an implicit message to other developers to go through this method, but I would like a bit more control if possible.

  • 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-06-19T03:58:51+00:00Added an answer on June 19, 2026 at 3:58 am

    You don’t want to override - init (if not for some other reason) – - init is not the method that creates the instance. You want to override + alloc for this:

    @implementation SingletonClass
    
    + (id)alloc
    {
        static id instance = nil;
        if (instance == nil) {
            instance = [super alloc];
        }
        return instance;
    }
    
    @end
    

    This way you can actually prevent (almost) completely creating multiple instances of SingletonClass.

    (Unless somebody falls back to calling

    id trickyDifferentInstance = class_createInstance(objc_getClass("SingletonClass"), 0));
    

    but that’s very unlikely.)

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

Sidebar

Related Questions

I have an application in java that uses two different threads.And for sharing data
I have some feature toggles that I include in my .NET application which uses
I have a singleton that I use for creating an application wide report. As
I've extended Application and declared a public ArrayList there that I use to hold
I've got an application that's using a static library I made. One .cpp file
The structure of application is that I have two Singletons. One for managing the
I have an application that keeps some complex data in memory between activities. As
In our application we use a singleton for a web service and that singleton
I have an Android application, which makes use of an AlarmManager to schedule events.
When we want deploy Web Application should we use singleton Object or use Static

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.