ok ill give it a shot here. i know Dharma also used a singleton in his latest app so maybe he can post some code also.
To start with, i relate the singleton as a poor-mans database. the only thing is that the singleton is initialized once (and only once) and persists in memory until the app is terminated. So it is great for small data amounts and variables.
wiki defines it as such :
In software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system.
I had a need in the aPokerTimer app to reference information from any view. passing all those variables was a big pain in the ***.
I also had to be able to save the state from any view at anytime in order to restore state in case of app backgrounding/termination. This made it easy to centralize all of my data and then call it to encode and decode the data.
so i used this:
SharedManager.h
#import <Foundation/Foundation.h>
@interface SharedManager : NSObject
{
NSMutableArray *sharedActiveBlinds;
NSMutableArray *sharedMasterStructureList;
NSMutableArray *payOutArray;
int players;
int buyin;
int rebuys;
int prize;
int firstIndex;
int secondIndex;
int thirdIndex;
int mins;
int secs;
int timeCount;
BOOL timerToggle;
BOOL alertTimerActive;
int currentLevel;
BOOL didDecode;
BOOL appLoad;
}
@property (nonatomic, retain) NSMutableArray *payOutArray,*sharedActiveBlinds, *sharedMasterStructureList;
@property (nonatomic) int players,buyin,rebuys,prize,firstIndex, secondIndex, thirdIndex, mins, secs, timeCount,currentLevel;
@property (nonatomic) BOOL timerToggle,alertTimerActive,didDecode, appLoad;
+ (SharedManager *)sharedObjects;
@end
SharedManger.m
#import "SharedManager.h"
static SharedManager *sharedInstance = nil;
@implementation SharedManager
@synthesize sharedActiveBlinds,sharedMasterStructureList, payOutArray;
@synthesize firstIndex, secondIndex, thirdIndex, mins, secs,timeCount, currentLevel,players,buyin,rebuys,prize;
@synthesize timerToggle,alertTimerActive,didDecode,appLoad;
+ (SharedManager *)sharedObjects
{
@synchronized (self) {
if (sharedInstance == nil) {
[[self alloc] init]; // assignment not done here, see allocWithZone
}
}
return sharedInstance;
}
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if (sharedInstance == nil) {
sharedInstance = [super allocWithZone:zone];
return sharedInstance; // assignment and return on first allocation
}
}
return nil; //on subsequent allocation attempts return nil
}
- (id)copyWithZone:(NSZone *)zone
{
return self;
}
- (id)retain
{
return self;
}
- (void)release
{
//do nothing
}
- (id)autorelease
{
return self;
}
- (NSUInteger)retainCount
{
return NSUIntegerMax; // This is sooo not zero
}
- (id)init
{
@synchronized(self) {
[super init];
payOutArray = [[NSMutableArray alloc]init];
sharedActiveBlinds = [[NSMutableArray alloc] init];
sharedMasterStructureList = [[NSMutableArray alloc] init];
firstIndex = 0;
secondIndex = 0;
thirdIndex = 0;
players = 0;
buyin = 0;
rebuys = 0;
return self;
}
}
@end
the only thing you really need to do is set up your variables like normal and synthesize the accessors. set up your variables in the init method with any defaults variables. Once that is done you can set/get the variables from any view Controller.
you can see that i haven't altered much of the original code.
in the .h file i set a number of instance variables that i want to store data in from any view i am in. i simple #import SharedManager.h into the required viewController .m file and there ya go. (standard)
in the .m file
it is creating a Static instance of the shared manager first
static SharedManager *sharedInstance = nil;
To get access to the sharedManager you use the following.:
SharedManager * sharedManager = [SharedManager sharedObjects];
The sharedObjects method ( and its allocWithZone) basically makes sure there is only instance floating around.
if you haven't already done so, you should read the blog about singletons by Matt Galloway. it really explains this better than i can.
here is an example of calling the code:
#import "MainVC.h"
#import "SharedManager.h"
-(IBAction) startTimer:(id)sender
{ SharedManager *sharedManager = [SharedManager sharedObjects];
if (sharedManager.timerToggle == NO)
{
sharedManager.alertTimerActive = YES;
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector (updateLabels:) userInfo:nil repeats:YES];
sharedManager.timerToggle = YES;
[self createPauseToolbar];
// //NSLog(@"Timer started");
}
else if (sharedManager.timerToggle == YES)
{
sharedManager.alertTimerActive = NO;
[timer invalidate];
timer = nil;
// //NSLog(@"Timer Paused");
sharedManager.timerToggle = NO;
[self createPlayToolbar];
}
}
Calling the shared manager is the same anywhere you need it.
If you have any questions i can certainly try to answer them. but really read matts blog, it helped me alot.
Greg