从 NSOperationQueue 取消 NSOperation [英] Cancel NSOperation From NSOperationQueue

查看:59
本文介绍了从 NSOperationQueue 取消 NSOperation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从一个 url 下载了信息,我将这个 url 作为 NSOperation 发送到 NSOperationQueue 中,我想知道,我如何删除特定的 NSOperation 来下载特定 url 的数据,现在我正在这样做:

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];
            }
        }
    }

我在 tableview 中有信息,所以当我删除索引路径的一行时,我在此检查中输入,并在 [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?

推荐答案

根据苹果指南,取消操作不会立即强制它停止正在执行的操作.尽管所有操作都期望尊重 isCancelled 返回的值,但您的代码必须显式检查此方法返回的值并根据需要中止.

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.

Justin 建议的示例......这是根据需要中止方法执行的方式.

Example as Justin Suggested....This is way abort execution of method as needed.

- (void)main
 { \\ ...do work... 
   if (self.isCancelled)
      { \\ ...get out...  } 
 } 

苹果的 NSOperation 类参考

这篇关于从 NSOperationQueue 取消 NSOperation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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