在NSURLConnection处于活动状态时点击主页按钮后,iPhone IOS目标InvalidException

时间:2012-10-24 13:46:58

标签: iphone multithreading performselector

我无法理解异常,在按下主页按钮后几秒钟就会抛出异常。

*** Terminating app due to uncaught exception 'NSDestinationInvalidException', reason: '*** -[GMMLoader performSelector:onThread:withObject:waitUntilDone:modes:]: target thread exited while waiting for the perform'

我正在编写一个应用程序,它使用Web服务来加载不同的数据集。 web服务被添加到operationQueue。

WebService* op = 
    [[WebService alloc] initWithSearchString:self.searchBar.text
      notificationCenter:self.progressNotification];
    [self.queue addOperation:op];

[op addObserver:self 
         forKeyPath:@"isFinished" 
            options:NSKeyValueObservingOptionNew 
            context:NULL];

[op release];

在Web服务的-(void) start消息中,我将此启动选择器称为在主线程中执行,以便能够与UI进行交互。

- (void)start
{
    if (![NSThread isMainThread]){

        [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:YES];
        return;
    }
    ...
}

所有这一切都运行正常,但当我在网络服务处于活动状态时按下主页按钮时,我得到了上述异常。

我的猜测是,按下主页按钮后主线程停止了(是这样吗?)。因此,当前正在运行的操作不再更新/有效。

我试图通过取消或暂停队列中的操作来听取事件UIApplicationWillResignActiveNotification对主页按钮的反应。然而,这没有帮助。我猜对了,这个错误是因为主线程停止了吗? 我怎么能设法停止/取消当前的webservice线程?

提前谢谢你 Zerd

1 个答案:

答案 0 :(得分:0)

我发现了错误。 事实证明,这不是Web服务本身的问题,而是使用地理定位器,它已经创建并启动了两次。

由于这个原因,创建了某种僵尸线程,它永远不会停止。