I have an ios 6 app which instantiates 3 singletons in the App Delegate as below:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Constants *constSingleton = [Constants getSingleton];
EntryContainerSingleton * eSingle = [EntryContainerSingleton getSingleton];
LocationMgrSingleton *loc = [LocationMgrSingleton getSingleton];
return YES;
}
However the problem happens that all three calls are executing in different threads simultaneously. EntryContainerSingleton depends on Constants to do some tasks. But Constants is not instantiated completely when it is executing those tasks.
How can I handle this situation? I was googling around and in previous versions of iOS people have used the NSOperation queue to do this.
However, Im not sure whether this is a good approach in iOS 6. And even if it is I haven't used NSOperation queue before and all the samples on the web are from previous versions and are instantiated in some class which is not an APP Delegate.
If someone can give me some sample code on how to do this in App Delegate to get me started Id really appreciate it
EDIT
Entries controller singleton
-(id)init
{
self = [super init];
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
[self getEntriesFromServer];
..............
.............
constSingleton = [Constants getSingleton];
[self addSelfAsObserverToNotifications];
return self;
}
Inside entriescontrollersingleton
-(void)getEntriesFromServer
{
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
if(!constSingleton)
{
NSLog(@"constSingleton is null");
}
__block NSString *dateString1 = [constSingleton getCurrentDateTime];
NSLog(@"Sending notification: Async Call started successfully at time %@", dateString1);
[[NSNotificationCenter defaultCenter] postNotificationName:@"didStartAsyncProcess"
object:self];
.......
}
Console output
[96894:c07] -[AppDelegate application:didFinishLaunchingWithOptions:] 21
[96894:c07] +[Constants getSingleton] 39
[96894:c07] -[Constants init] 65
[96894:c07] -[EntryContainerSingleton init] 75
[96894:c07] -[EntryContainerSingleton getEntriesFromServer] 154
[96894:c07] constSingleton is null
[96894:c07] Sending notification: Async Call started successfully at time (null)
[96894:c07] -[Constants incrementNumBackgroundNetworkTasksBy1:] 87