Using a Data Management Singleton

Posted by Dan Ray on Stack Overflow See other posts from Stack Overflow or by Dan Ray
Published on 2010-05-10T13:19:51Z Indexed on 2010/05/10 13:24 UTC
Read the original article Hit count: 188

Filed under:
|
|

Here's my singleton code (pretty much boilerplate):

@interface DataManager : NSObject {
    NSMutableArray *eventList;
}

@property (nonatomic, retain) NSMutableArray *eventList;

+(DataManager*)sharedDataManager;

@end

And then the .m:

#import "DataManager.h"

static DataManager *singletonDataManager = nil;

@implementation DataManager

@synthesize eventList;

+(DataManager*)sharedDataManager {
    @synchronized(self) {
        if (!singletonDataManager) {
            singletonDataManager = [[DataManager alloc] init];
        }
    }
    NSLog(@"Pulling a copy of shared manager.");
    return singletonDataManager;
}

So then in my AppDelegate, I load some stuff before launching my first view:

NSMutableArray *eventList = [DataManager sharedDataManager].eventList;

....

NSLog(@"Adding event %@ to eventList", event.title);
[eventList addObject:event];
NSLog(@"eventList now has %d members", [eventList count]);
[event release];

As you can see, I've peppered the code with NSLog love notes to myself. The output to the Log reads like:

2010-05-10 09:08:53.355 MyApp[2037:207] Adding event Woofstock Music Festival to eventList
2010-05-10 09:08:53.355 MyApp[2037:207] eventList now has 0 members
2010-05-10 09:08:53.411 MyApp[2037:207] Adding event Test Event for Staging to eventList
2010-05-10 09:08:53.411 MyApp[2037:207] eventList now has 0 members
2010-05-10 09:08:53.467 MyApp[2037:207] Adding event Montgomery Event to eventList
2010-05-10 09:08:53.467 MyApp[2037:207] eventList now has 0 members
2010-05-10 09:08:53.524 MyApp[2037:207] Adding event Alamance County Event For June to eventList
2010-05-10 09:08:53.524 MyApp[2037:207] eventList now has 0 members

... What gives? I have no errors getting to my eventList NSMutableArray. But I addObject: fails silently?

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c