1

对于 iPhone 应用程序,我使用 NSOperationQueue 来限制对 SQLite 数据库的访问,一次只能进行一个查询。我创建了一个 NSOperation 的子类,在主函数中我有以下内容:

- (void)main
{    
    // ... other code here ...

    if( [_delegate respondsToSelector:@selector(queryCompleted:)] )
    {
        [_delegate performSelectorOnMainThread:@selector(queryCompleted:) 
                                    withObject:self
                                 waitUntilDone:NO];
    }
}

代表方:

- (void)queryCompleted:(QueryOperation*)aQueryOperation
{
     // Breakpoint here allows me to explore and see the members of aQueryOperation

     id results = [aQueryOperation resultSet]; // Crashes here

     // ... more code here ...
}

我传递的原因self是允许委托访问查询操作的 ID(在每个委托打开多个请求的情况下)和查询结果。

在 的文档中performSelectorOnMainThread:withObject:waitUntilDone:,它明确指出:

“此方法保留接收器和 arg 参数,直到执行选择器之后。”

但是,当委托方法尝试访问参数时,会引发“EXC_BAD_ACCESS”异常。关于为什么的任何想法?

奇怪的是,如果我在对 NSOperation 对象的崩溃引用之前设置断点,调试器允许我查看对象实例和所有参数的值。

4

1 回答 1

0

尝试将 waitUntilDone: 参数设置为 YES。可能存在允许 NSOperation 自行释放的竞争条件。

于 2010-06-02T10:06:24.330 回答