NSOperation 传递给委托时导致崩溃
对于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将 waitUntilDone: 参数设置为 YES。可能存在竞争条件,允许 NSOperation 自行释放分配。
Try setting the waitUntilDone: parameter to YES. Possibly there is a race condition which is allowing the NSOperation to deallocate itself.