我正在使用线程开发应用程序,并且发生了一些奇怪的事情。
我有这些方法:
-(void)loadSelectedTest:(int)idTest mustBeSolved:(BOOL)mustBeSolved
{
NSThread *thread2 = [[NSThread alloc] initWithTarget:self selector:@selector(loadTestAnimation) object:nil];
[thread2 start];
[respuestas loadTestQuestions:idTest testWillBeSolved:mustBeSolved];
[thread2 cancel];
[thread2 release];
sleep([progressAnimation animationDelayForAnimationId:lastSelectedAnimationId]);
[progressAnimation removeFromSuperview];
}
-(void)loadTestAnimation
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
progressAnimation = [[OTTestProgressAnimation alloc] init];
progressAnimation.frame = respuestas.view.frame;
[self.view addSubview:progressAnimation];
[progressAnimation loadWithAnimationWithId:lastSelectedAnimationId title:lastSelectedTestTitle subtitle:lastSelectedTestSubtitle];
[progressAnimation release];
[pool release];
}
-(void)loadSavedTest:(int)idTest mustBeSolved:(BOOL)mustBeSolved
{
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(loadTestAnimation) object:nil];
[thread start];
[respuestas loadSavedTestQuestions:lastSelectedTestId testWillBeSolved:YES];
[thread cancel];
[thread release];
sleep([progressAnimation animationDelayForAnimationId:lastSelectedAnimationId]);
[progressAnimation removeFromSuperview];
}
loadSelectedTest 和 loadSavedTest 遵循以下逻辑:
在主线程加载所选项目时打开一个加载进度操作屏幕的线程。
当调用方法 loadSelectedTest 时,一切正常,出现等待屏幕,当测试加载并且睡眠时间结束时,进度屏幕被卸载并且所选测试出现在屏幕中。
但是当调用 loadSavedTest 方法时,首先执行加载并显示测试,然后显示进度屏幕。
为什么在第一种情况下线程顺序正确执行而在第二种情况下没有?
我失去了什么?
谢谢。