我有一個IOS 6的應用程序,其實例在App代表3個單身如下:手柄對象相互依賴6應用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
Constants *constSingleton = [Constants getSingleton];
EntryContainerSingleton * eSingle = [EntryContainerSingleton getSingleton];
LocationMgrSingleton *loc = [LocationMgrSingleton getSingleton];
return YES;
}
但是該問題發生的所有三種呼叫在不同的線程同時執行。 EntryContainerSingleton依賴於常量來完成一些任務。但是在執行這些任務時,常量沒有完全實例化。
我該如何處理這種情況? 我在google上搜索,在以前的iOS版本中,人們使用NSOperation隊列來做到這一點。
但是,我不知道這是否是iOS 6中的一個好方法。即使是我之前沒有使用過NSOperation隊列,網絡上的所有樣本都來自以前的版本,並且在某些類中實例化不是APP代表。
如果有人能給我如何做到這一點的應用代表讓我開始編號一些示例代碼真的很感激
編輯
條目控制器單
-(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];
.......
}
控制檯輸出
[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
3個調用在不同的線程中如何執行?它看起來像是在單個線程上運行,依次在'application:didFinishLaunchingWithOptions:'中運行。你能澄清一下究竟發生了什麼,問題是什麼? – monoxygen 2013-02-15 03:38:20
我認爲他們是3個不同的線程,因爲我在他們的函數調用中有一些打印語句,而控制檯輸出顯示在上面添加的編輯部分中的上面。正如你可以看到一個對象被實例化並部分完成,然後另一個被啓動 – banditKing 2013-02-15 03:40:43
當條目單例調用方法getentriesfromserver時,它需要常量singleton,但常量singleton當時爲空 – banditKing 2013-02-15 03:43:45