我有一个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