打开应用程序(appDelegate/appDidBecomeActive)iPhone SDK时加载2个具有依赖项的单例
我正在尝试加载两个标准问题样式的单件:当我的iPhone应用程序加载时。这是我的密码:打开应用程序(appDelegate/appDidBecomeActive)iPhone SDK时加载2个具有依赖项的单例,iphone,objective-c,ios4,singleton,race-condition,Iphone,Objective C,Ios4,Singleton,Race Condition,我正在尝试加载两个标准问题样式的单件:当我的iPhone应用程序加载时。这是我的密码: - (void) applicationDidFinishLaunching:(UIApplication *)application { // first, restore user prefs [AppState loadState]; // then, initialize the camera [[CameraModule sharedCameraModule]
- (void) applicationDidFinishLaunching:(UIApplication *)application {
// first, restore user prefs
[AppState loadState];
// then, initialize the camera
[[CameraModule sharedCameraModule] initCamera];
}
我的相机模块具有检查AppState单例属性的代码。但我认为发生的是一个竞赛情况,相机模块SuntLon试图在AppStand属性初始化的时候访问它,所以属性是NIL,它正在重新初始化AppStand。我真的很想把它们分开,而不是把一个或两个都扔进App委托中。还有人见过这样的东西吗?您使用了什么样的解决方法,或者您有什么建议
提前谢谢
以下是loadState方法:
+ (void)loadState {
@synchronized([AppState class]) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *file = [documentsDirectory stringByAppendingPathComponent:@"prefs.archive"];
Boolean saveFileExists = [[NSFileManager defaultManager] fileExistsAtPath:file];
if(saveFileExists) {
sharedAppState = [[NSKeyedUnarchiver unarchiveObjectWithFile:file] retain];
} else {
[AppState sharedAppState];
}
}
}
在CameraModule中调用AppState的行:
- (void)initCamera {
...
if([AppState sharedAppState].captureSession != nil) {
...
}
}
最奇怪的事情发生了。。。我在我的RootViewController中访问AppState的一个属性,而应用程序的执行顺序是罪魁祸首 RootViewController加载到我的MainWindow.xib文件中,显然,xib加载/shenanegans发生在ApplicationIDFinishLaunching中的任何内容之前,因此loadState在游戏后期被调用
解决方案是将[AppState loadState]移动到我的RootViewController的init方法。你的loadState代码是什么样子的?除非您有意在后台线程上执行工作,否则该方法在完成之前不会返回。您好,谢谢您的帮助-我刚刚在问题中添加了loadState方法。sharedAppState是什么?您能从访问Appstate属性的CameraModule发布代码吗?sharedAppState是singleton的实例。。。查看我帖子中的链接。我正在使用页面中间的synthesisSingleton.h文件。当然,我会发布AppState属性行。