2

我正在从后台线程执行几个步骤,在每个使用完成时更新视图:

[self performSelectorOnMainThread:@selector(updateProgress) withObject:nil waitUntilDone:NO];

问题是如果用户关闭了这个视图。后台线程继续运行,并在执行这些操作之一时使应用程序崩溃。如何判断线程是否已消失?

4

1 回答 1

1

Do you intend run method updateProgress in Main thread or Background thread? This is simple trick to check. For the full proof you should use NSOperationQueue instead of performSelector

synthesize this BOOL in your viewController

BOOL isBackgroundThreadStop;

initialize to NO somewhere relevant

self.isBackgroundThreadStop = NO;

When user pressed button dismiss the view:

-(void)dismissView
{   
    self.isBackgroundThreadStop = YES;
    [self dismissModalViewControllerWhatEver];
}

So in your method

-(void)updateProgress
{
    if(!self.isBackgroundThreadStop){
    //run program if background thread not stop
    //it won't run if isBackgroundThreadStop is set to YES
    }
}
于 2013-02-21T12:19:35.013 回答