English 中文(简体)
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 withObject: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.





相关问题
Ruby Interpreter crashes with a certain word

Ok, this one s a little ridiculous, and I m almost afraid no one will believe me. But here it goes: I have written a Ruby Rails application that handles content for tons of domains. Now I know this ...

Java HotSpot error

Curious if anyone could help out in regards to a Java HotSpot dump...saw some reference to head over to the Sun Forums, figured I would try here first...below is the dump... # # An unexpected error ...

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

Is this kind of crash report useless?

I tried use "PLCrashReport" to collect the crash info then make the app more stable, but turns out that the report is like this(dont even has a call stack,how am I suppose to use it?): The "Exception:...

Xcode crashes with divide by zero

I downloaded urlcache.zip from http://developer.apple.com/iphone/library/samplecode/URLCache/index.html#//apple_ref/doc/uid/DTS40008061 I opened the project in xcode and clicked on urlcacheconection....

热门标签