iPhone模拟器挂起在NSLog上

时间:2014-04-25 12:09:15

标签: ios objective-c singleton ios-simulator

我在这里遇到了一个奇怪的问题。我在Sprite Kit中创建游戏,当我第一次初始化所有变量时,似乎工作正常。我有一个单例类,它包含我将在游戏中使用的所有数据。这个单例包含一个对象(World),World对象包含我在整个游戏中使用的对象数组。

初始化后,我尝试打印出某个数组中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

我不会认为&#39;保留&#39;或者任何事情会引起这种情况,但我不确定问题是什么。任何帮助是极大的赞赏。谢谢!

编辑::以下是我的单身人士的代码:

+(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;
}

1 个答案:

答案 0 :(得分:0)

要调试任何挂起,首先要做的就是所有线程的堆栈跟踪。当在iOS模拟器中发生这种情况时,按下Xcode中的“Pause”按钮并查看堆栈跟踪。您还可以在调试器窗口中键入t a a bt(这意味着线程应用所有回溯)以转储所有线程的堆栈跟踪。

完成后,查看主线程被阻止的内容(例如,死锁,阻塞i / o,无限递归)。