NSOperation取消问题

时间:2011-08-05 10:32:57

标签: nsoperation nsoperationqueue

我有问题。在我的程序中,我需要在单击按钮后取消队列中的操作(子类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

1 个答案:

答案 0 :(得分:2)

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

if(self.isCancelled){
return;
}