NSOperationQueue和dealloc被调用并导致App崩溃的问题 [英] Issues with NSOperationQueue and dealloc being called and crashing App

查看:91
本文介绍了NSOperationQueue和dealloc被调用并导致App崩溃的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在队列中创建了一个NSOperation,就像这样:

I've created an NSOperation in the queue like so:

ImageLoadingOperation *operation = [[ImageLoadingOperation alloc] initWithImageURL:url target:self action:@selector(didFinishLoadingImageWithResult:)];
[operationQueue addOperation:operation];
[operation release];

这很好,但是如果在操作完成前弹出视图,则应用程序崩溃并显示"EXC_BAD_ACCESS"

And this works fine but if the view gets popped before the operation finishes the App crashes with "EXC_BAD_ACCESS"

我试图通过调用cancelAllOperations取消操作队列,但是由于它已经在处理中,因此无法防止应用崩溃. docos表示,如果该操作正在运行,则取决于该操作是否检测到该操作已被取消并做出适当的响应,但不太确定我将如何实施此操作?

I've tried to cancel the the operation Queue by calling cancelAllOperations but as its already in process it doesn't prevent the App from crashing. The docos say that if the operation is running it is up to the operation to detect that it has been canceled and respond appropriately but not too sure how I would implement this?

有什么想法吗?

推荐答案

这是View调用某些网络然后进行回调的普遍问题.

It is a general problem for View calling some network and then callback.

我的解决方案是您可以在调用操作之前保留视图.然后,当操作完成时,您释放视图.

My solution is you can retain the view before you call the operation. And then, when the operation finishes, you release the view.

- (void)longTask {
   [self retain];
}

- (void)longTaskDidFinish {
   // do something if you want
   [self release];
}

这篇关于NSOperationQueue和dealloc被调用并导致App崩溃的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆