I am trying to create an NSOperation by subclassing from NSOperation.I want my operation to be executed on a separate thread as well as it should support canceling option i.e i should be able to stop the thread(or the operation) at any point of time.I tried adding my operation instance to NSOperationQueue,everything is working fine but 开发者_如何转开发"the operation" is being executed after some time which makes my application slow.So i tried running my operation alone by calling [theOperation start];
its pretty fast but executing on the main thread.How to make the NSOperation run on separate thread with canceling option please help.
either:
1) do not make it an NSOperation and explicitly create a thread.
2) use a second operation queue and/or increase the priority.
3) use a container type for the result. when you need it immediately, cancel the background load operation and load manually.
I believe you need to implement - (BOOL)isConcurrent
:
- (BOOL)isConcurrent
{
return YES;
}
But there are implications. Read the Apple reference docs on NSOperation, particularly the section Subclassing Notes. Also note that if you want the operation to be cancellable, you need to implement the cancelling behaviour yourself: eg, in your main
selector you need to check if the operation has been cancelled, and immediately return if it has.
精彩评论