2011-08-05 79 views
1

我有一個問題。在我的程序中,我需要在點擊按鈕後取消隊列中的操作(子類NSOperation)。但是當我調用[隊列cancelAllOperations]時,沒有任何反應。隊列繼續執行。所有操作必須是併發的。我究竟做錯了什麼?NSOperation取消問題

@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 

回答

2

你需要在你的代碼來檢查,如果你的操作被取消與中斷它:

if(self.isCancelled){ 
return; 
} 
+0

沒錯! 我還建議閱讀本文件: http://developer.apple.com/library/ios/#technotes/tn2109/_index.html – Ricardo