NSOperation 传递给委托时导致崩溃

发布于 2024-09-03 08:55:43 字数 1314 浏览 1 评论 0原文

对于 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 对象的崩溃引用之前设置断点,调试器允许我查看对象实例和所有参数的值。

For an iPhone app, I'm using a NSOperationQueue to limit access to the SQLite database to one query at a time. I created a subclass of NSOperation and in the main function I have the following:

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

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

Delegate side:

- (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 ...
}

The reason I am passing self is to allow the delegate to access the ID for the query operation (in the case where there is more than one request open per delegate) and the results from the query.

In the documentation for performSelectorOnMainThread:withObject:waitUntilDone:, it clearly states:

"This method retains the receiver and the arg parameter until after the selector is performed."

However when the delegate method tries to access the argument, an "EXC_BAD_ACCESS" exception is thrown. Any thoughts on why?

Oddly enough, if I set a breakpoint before the crashing reference to the NSOperation object, the debugger permits me to see the object instance and the values of all the parameters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-09-10 08:55:43

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

Try setting the waitUntilDone: parameter to YES. Possibly there is a race condition which is allowing the NSOperation to deallocate itself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文