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]);
}
Sending
[[MySingleton alloc] init]from another class completely negates the purpose of a singleton. You should only be accessing the singleton via thesharedSingletonclass 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 byHomeViewController, inXMLParser‘s-initmethod.Your
-parser:didEndElement:namespaceURI:qualifiedName:method is setting session data on a brand new instance ofSingletonand not the shared instance that is accessed via thesharedInstanceclass method. OnceHomeViewControllerreceives the notification, it checks the shared instance and the properties are nil because the wrong instance was being populated. Remove the-initmethod andsingletonivar fromXMLParserand only use[Singleton sharedInstance]instead.