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

  • Home
  • SEARCH
  • 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 6919163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:57:17+00:00 2026-05-27T09:57:17+00:00

I have a singleton called Singleton that manages certain variables needed across the application

  • 0

I have a singleton called Singleton that manages certain variables needed across the application for me.

I am using a View Controller HomeViewController which initializes the singleton on viewDidLoad and then sends a message to a server and receives information. It then takes that information and parses it with a separate class XMLParser.

When the XMLParser is done parsing it sends a notification to HomeViewController that it is done, and then HomeViewController dumps the data into NSLog.

In my XMLParser class, I call [singleton setXX:XX] and then NSLog(@"%@", [[singleton XX]description]); which dumps the data perfectly. If I go back to HomeViewController (after the notification posts) and try to log that same data it returns Null.

I tried not initializing the singleton in viewDidLoad and moving the initialization until after I receive the notification saying that the parse is complete, but I still get Null. Any ideas? I’m sure it has something to do with memory management (ARC, btw), but I am not sure exactly where.

Edit: Here is my Singleton code.

//.h
//...
@property (nonatomic, retain) NSArray *linkedList;
@property (nonatomic, retain) NSDictionary *sessionData;

+ (id)sharedSingleton;

//.m
static MySingleton *sharedSingleton = nil;

@implementation MySingleton

@synthesize linkedList, sessionData;
+ (id)sharedSingleton {
    @synchronized(self) {
        if (sharedSingleton == nil)
            sharedSingleton = [[self alloc] init];
    }
    return sharedSingleton;
}

Edit2: Here are my singleton accessor methods, etc.

//XMLParser
- (id) init {
    self = [super init];
    if (self != nil) {
        if (!singleton) singleton = [[MySingleton alloc]init];
    }
    return self;
}
//...cut because no singleton access
//Closing Element
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
    if ([elementName isEqualToString:@"SessionData"])
    {
        NSLog(@"Completed with SessionData. ID:11116");
        [singleton setSessionData:sessionData];
        NSLog(@"SessionData Description: \n%@", [sessionData description]);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushToScreen" object:nil];
        return;
    }
}

//HomeViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    singleton = [MySingleton sharedSingleton];
}
//...cut for useless code
//Then when notification received, it calls this selector
- (void)connect
{
    //singleton = [MySingleton sharedSingleton];
    NSLog(@"%@", [[singleton linkedList]description]);
}
  • 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-27T09:57:18+00:00Added an answer on May 27, 2026 at 9:57 am

    Sending [[MySingleton alloc] init] from another class completely negates the purpose of a singleton. You should only be accessing the singleton via the sharedSingleton class reference. All of the logic pertaining to whether an instance has been established is encapsulated inside the singleton. This way you’re sure that you’re always receiving the same instance back.

    In your case, you’ve created a new instance of Singleton, completely bypassing the shared instance that is used by HomeViewController, in XMLParser‘s -init method.

    Your -parser:didEndElement:namespaceURI:qualifiedName: method is setting session data on a brand new instance of Singleton and not the shared instance that is accessed via the sharedInstance class method. Once HomeViewController receives the notification, it checks the shared instance and the properties are nil because the wrong instance was being populated. Remove the -init method and singleton ivar from XMLParser and only use [Singleton sharedInstance] instead.

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

Sidebar

Related Questions

The goal is to have a singleton data controller class called FetchData.h/.m that pulls
I have a singleton class called Manager that holds a list of object instances:
I have a singleton class, that has a map which can be accessed by
I have a singleton called SettingsManager in my app that should take care of
I have a singleton object called PoolManager that loads and saves some data in
I have an asynchronous method in my EJB singleton that's called from another method
I have a singleton that uses the static readonly T Instance = new T();
I have a Singleton that is accessed in my class via a static property
I have a singleton object that use another object (not singleton), to require some
Assume I have a singleton class in an external lib to my application. But

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.