我这里有一个奇怪的问题。我正在Sprite Kit中创建一个游戏,当我第一次初始化所有变量时,它似乎可以正常工作。我有一个单例类,其中包含将在游戏中使用的所有数据。这个单例包含一个对象(世界),而世界对象包含我在整个游戏中使用的对象数组。
初始化之后,我尝试打印出World对象包含在某个数组中的内容。下面是一个示例:
NSLog(@"Game manager ports: %@", [IPGameManager sharedGameData].world.ports);
但是,运行此时,模拟器将挂起。我还没有在真实设备上尝试过(还没有开发者帐户)。但是,当我在初始化World'ports'之后立即打印相同的内容时,它可以正常打印。这是World对象的头文件:
@interface IPWorld : NSObject <NSCoding>
@property (nonatomic, retain) IPPlayer* player;
@property (nonatomic, retain) NSMutableArray* ports;
@property (nonatomic, retain) NSMutableArray* crewMembers;
@property (nonatomic, retain) NSMutableArray* ships;
@property (nonatomic, retain) NSMutableArray* quests;
@end
我不认为“保留”或任何原因会导致这种情况,但我不确定是什么问题。任何帮助是极大的赞赏。谢谢!
编辑::下面是我单身的代码:
+(instancetype)sharedGameData {
NSLog(@"Loading sharedGameData");
static id sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"should only see this once");
sharedInstance = [self loadInstance];
});
return sharedInstance;
}
+(instancetype)loadInstance {
NSData* decodeData = [NSData dataWithContentsOfFile:[IPGameManager filePath]];
if (decodeData) {
IPGameManager* gameData = [NSKeyedUnarchiver unarchiveObjectWithData:decodeData];
return gameData;
}
return [[IPGameManager alloc] init];
}
+(NSString*)filePath {
static NSString* filePath = nil;
if (!filePath) {
filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"gamedata"];
}
return filePath;
}
要调试任何挂起,您需要做的第一件事是所有线程的堆栈跟踪。在iOS模拟器中发生这种情况时,请按Xcode中的“暂停”按钮,然后查看堆栈跟踪。您还可以在调试器窗口中键入t a a bt
(这意味着线程将应用所有回溯)转储所有线程的堆栈跟踪。
一旦有了它,就可以查看主线程的阻塞状态(例如,死锁,阻塞I / O,无限递归)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句