0

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
banditKing
  • 9,405
  • 28
  • 100
  • 157
  • How are the 3 calls executing in different threads? It looks like they are running on a single thread, sequentially, in `application:didFinishLaunchingWithOptions:`. Can you clarify what is actually going on and what the problem is? – monoxygen Feb 15 '13 at 03:38
  • I assumed they are 3 different threads because I have some print statements in their functions calls and the console output shows above in the edited part I added above. As you can see one object is instantiated and partly done then another is started – banditKing Feb 15 '13 at 03:40
  • When Entries singleton calls method getentriesfromserver, it needs constants singleton but constants singleton is null at that time – banditKing Feb 15 '13 at 03:43
  • which object is printing `constSingleton is null` on the console? – didierc Feb 15 '13 at 03:56
  • entriescontainer is printing that line within the getentriesfromserver method. see the edit I made above – banditKing Feb 15 '13 at 03:57

1 Answers1

0

If the Entries singleton needs access to the Constants singleton, it should call [Constants getSingleton] to obtain it. Make sure you implement your singleton methods correctly (see Objective-C Singleton implementation, am i doing it right?)

Every time you need access to a singleton object, you should call [ClassName getSingleton]. There shouldn't be any reason to store a singleton as an instance variable in your application delegate.

Community
  • 1
  • 1
monoxygen
  • 453
  • 3
  • 14
  • Yes this is exactly how its being done right now, and thats exactly why Im posting this question. My entries singleton has the method [Constants getSingleton] and after that it calls a method on the constants class. However, constants singleton is not insitialized at that point. Thats exactly the problem – banditKing Feb 15 '13 at 03:56
  • If you have implemented your singleton method correctly, it should return an initialized singleton. See the link in the answer. – monoxygen Feb 15 '13 at 04:02
  • Never mind. I think I found what the problem was. D-Uh Im so dumb!! I was getting a ref to constants singleton after I called entries from server!! – banditKing Feb 15 '13 at 04:05