问题
I have downloaded information from an url, and I am sending this url as NSOperation in an NSOperationQueue, I want to know, how I can delete a specific NSOperation to download data for a specific url, now I am doing in this way:
AppDelegate *appController = (AppDelegate *) [[UIApplication sharedApplication] delegate];
for (NSOperation *op in appController.seriesQueue.operations) {
if ([op isKindOfClass:[MyDowndload class]]) {
MyDownload *urlInDownload = (MyDowndload *)op;
if ([urlInDownload.myNew.urlName isEqualToString:[managedObject valueForKey:@"urlName"]] && [urlInDownload.myNew.language isEqualToString:[managedObject valueForKey:@"language"]]) {
[op cancel];
}
}
}
I have the information in a tableview, so when I delete a row for index path I enter in this check, and enter also in the [op cancel] line, but I can see in the console log that the thread is still downloading, how can I stop and delete it?
回答1:
According to apple guideline, Canceling an operation does not immediately force it to stop what it is doing. Although respecting the value returned by the isCancelled is expected of all operations, your code must explicitly check the value returned by this method and abort as needed.
Example as Justin Suggested....This is way abort execution of method as needed.
- (void)main
{ \\ ...do work...
if (self.isCancelled)
{ \\ ...get out... }
}
NSOperation Class Reference by apple
回答2:
To stop downloading the request operation u need to kill/stop the runloop of that request.
To find the specific operaton in operation queue I think the above operation is the simple one allowed.
if you need more control,
create a subclass of nsoperation and use tag for each operation so that you can find specific operation quickly by checking with unique tag.
And put a custom KVO in the subclass of operation to quiet the operation immediately when needed. I mean override the start method of that thread.
thanks,
Naveen Shan
来源:https://stackoverflow.com/questions/10313717/cancel-nsoperation-from-nsoperationqueue