XCode 5 を使用しています。Cocos2D を使用してゲームを作成しています。また、いくつかの関数を順番に繰り返し実行したり、次々と実行したりします。一度に多くのアクションが実行されるため、遅延も必要です (これにより、アクションが次のアクションに干渉されず、前のアクションの効果をキャンセルしたり、再スケジュールして機能をすぐに実行したりしません)。
インターフェイスがフリーズし、Debug Navigator で CPU 消費が 99 ~ 100% に達し、メモリが徐々に増加し、増加し続けます。すべての例外ブレークポイントも配置します。しかし、例外は発生しません。
私はこのコードを使用しています
-(void)threadMainRunLoop
{
BOOL exitNow = NO;
int loopCount = 1;
[self function1];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
// Add the exitNow BOOL to the thread dictionary.
NSMutableDictionary *threadDict = [[NSThread currentThread] threadDictionary];
[threadDict setValue:[NSNumber numberWithBool:exitNow] forKey:@"ThreadShouldExitNow"];
// Install an input source.
[NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(function2) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(function3) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(function4) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:@selector(function5) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:@selector(function6) userInfo:nil repeats:NO];
while (loopCount <= 4 && !exitNow)
{
// Do one chunk of a larger body of work here.
// Change the value of the moreWorkToDo Boolean when done.
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(function2) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(function3) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(function4) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:0.7 target:self selector:@selector(function5) userInfo:nil repeats:NO];
[NSTimer scheduledTimerWithTimeInterval:1.2 target:self selector:@selector(function6) userInfo:nil repeats:NO];
// Run the run loop but timeout immediately if the input source isn't waiting to fire.
[runLoop runUntilDate:[NSDate dateWithTimeInterval:5.0 sinceDate:[NSDate date]] ];
// Check to see if an input source handler changed the exitNow value.
exitNow = [[threadDict valueForKey:@"ThreadShouldExitNow"] boolValue];
++loopCount;
}
}
そして、この関数を で呼び出しますccTouchesEnded:withEvent:
。
「Listing 2-3 Checking for an exit condition during a long job」のスレッド管理からこのコードをたどりました。
私の NSRunLoop は私のゲーム/インターフェースをフリーズさせますか???
デバイスがハングしていないことを覚えています。function1-6 は UI を更新しています。また、ゲームがフリーズする時期は予期されていません。
前もって感謝します。