以下のように、AppDelegateで3つのシングルトンをインスタンス化するiOS6アプリがあります。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Constants *constSingleton = [Constants getSingleton];
EntryContainerSingleton * eSingle = [EntryContainerSingleton getSingleton];
LocationMgrSingleton *loc = [LocationMgrSingleton getSingleton];
return YES;
}
ただし、3つの呼び出しすべてが異なるスレッドで同時に実行されているという問題が発生します。EntryContainerSingletonは、いくつかのタスクを実行するために定数に依存しています。ただし、これらのタスクを実行しているときは、定数が完全にインスタンス化されるわけではありません。
この状況にどのように対処できますか?私はグーグルで遊んでいましたが、以前のバージョンのiOSでは、NSOperationキューを使用してこれを実行していました。
ただし、これがiOS 6で適切なアプローチであるかどうかはわかりません。また、以前にNSOperationキューを使用したことがなく、Web上のすべてのサンプルが以前のバージョンのものであり、APPではないクラスでインスタンス化されている場合でも委任します。
誰かが私を始めるためにAppDelegateでこれを行う方法についてのサンプルコードを私に与えることができれば、私は本当に感謝しています
編集
エントリーコントローラーシングルトン
-(id)init
{
self = [super init];
NSLog(@"%s %d", __PRETTY_FUNCTION__, __LINE__);
[self getEntriesFromServer];
..............
.............
constSingleton = [Constants getSingleton];
[self addSelfAsObserverToNotifications];
return self;
}
エントリの内部controllersingleton
-(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];
.......
}
コンソール出力
[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