开发者

NSOperation performSelectorOnMainThread crashes

I'm calling a NSOperation from a Subview of a NavigationController.

MyOperation *op = [[MyOperation alloc] target:self action:@selector(didFinishOperation)];

The Operation loads some data from a server, parses it and then sends a

[target performSelectorOnMainThread:action w开发者_如何学PythonithObject:nil waitUntilDone:YES];

when the job is done. This is generally working...

The problem is when I push the back button from the navigation controller while the NSOperation is doing its job, the app crashes because the action references a deallocated instance.

I've tried retaining the operation - but it still crashes...

Thanks in advance


Chris is on the right track, but it doesn't cover the case where the operation completes as you try to cancel it, and it will still crash. If the operation is executing in main, the cancel won't take effect.

Still, try and cancel it before your target is destroyed, but test that the target still exists before attempting to send it a message. Also, check if you're cancelled before trying to send the message:

if ( self.isCancelled ) return;


In your UIView dealloc method, you could cancel the operation and remove it from its NSOperationQueue.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜