NSOperation取消问题

发布于 2024-11-28 05:20:03 字数 1204 浏览 1 评论 0原文

我有一个问题。在我的程序中,我需要在单击按钮后取消队列中的操作(NSOperation 子类)。但是当我调用 [queue cancelAllOperations] 时什么也没有发生。队列继续执行。所有操作必须并发。我做错了什么?

 @interface SearchOperation : NSOperation 
{
TrueSearcherViewController *viewController;
SearchYouTube *you_search;
SearchGoogle *goo_search;
BOOL semafore;
}

- (id)initYoutubeTaskWithData:(SearchYouTube *) sy;
- (id)initGoogleTaskWithData:(SearchGoogle *) sg; 
- (void) beginYoutubeSearch:(SearchYouTube *) sy;
- (void) beginGoogleSearch:(SearchGoogle *) sg;
@end


#import "SearchOperation.h"

@implementation SearchOperation

- (void) start
{
if (semafore == YES)
    {
        [self beginYoutubeSearch:you_search];
    }
    else 
    {
        [self beginGoogleSearch:goo_search];
    }
}

- (id)initYoutubeTaskWithData:(SearchYouTube *) sy 
{
if (self = [super init])
{
    you_search = sy;
    semafore = YES;
}
return self;
}

- (id)initGoogleTaskWithData:(SearchGoogle *) sg
{
if (self = [super init])
{
    goo_search = sg;
    semafore = NO;
}
return self;
}


- (void) beginYoutubeSearch:(SearchYouTube *) sy
{
[sy runSearch];
}


- (void) beginGoogleSearch:(SearchGoogle *) sg
{
[sg runSearch];
}

- (void) dealloc
{
[super dealloc];
}

@end

I have a problem. In my program I need to cancel operations (subclass NSOperation) in queue after clicking on button. But when I calling [queue cancelAllOperations] nothing happens. The queue continue executing. All operations must be concurrent. What am I doing wrong?

 @interface SearchOperation : NSOperation 
{
TrueSearcherViewController *viewController;
SearchYouTube *you_search;
SearchGoogle *goo_search;
BOOL semafore;
}

- (id)initYoutubeTaskWithData:(SearchYouTube *) sy;
- (id)initGoogleTaskWithData:(SearchGoogle *) sg; 
- (void) beginYoutubeSearch:(SearchYouTube *) sy;
- (void) beginGoogleSearch:(SearchGoogle *) sg;
@end


#import "SearchOperation.h"

@implementation SearchOperation

- (void) start
{
if (semafore == YES)
    {
        [self beginYoutubeSearch:you_search];
    }
    else 
    {
        [self beginGoogleSearch:goo_search];
    }
}

- (id)initYoutubeTaskWithData:(SearchYouTube *) sy 
{
if (self = [super init])
{
    you_search = sy;
    semafore = YES;
}
return self;
}

- (id)initGoogleTaskWithData:(SearchGoogle *) sg
{
if (self = [super init])
{
    goo_search = sg;
    semafore = NO;
}
return self;
}


- (void) beginYoutubeSearch:(SearchYouTube *) sy
{
[sy runSearch];
}


- (void) beginGoogleSearch:(SearchGoogle *) sg
{
[sg runSearch];
}

- (void) dealloc
{
[super dealloc];
}

@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷心人i 2024-12-05 05:20:03

如果您的操作被取消,您需要签入代码以中断它:

if(self.isCancelled){
return;
}

You need to check in your code if your operation is cancelled to interrupt it with :

if(self.isCancelled){
return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文