2014-03-05 84 views
0

我使用的是網絡2.0顯示YouTube視頻。 當我有一個連接錯誤,我打開一個警報視圖,我想停止請求,如果我點擊「確定」。AFNetworking失敗塊

警報視圖顯示正確,但當我點擊「確定」的請求不會停止,我看到活動指標。

如何停止請求?

這是下面的代碼:

-(void)loadData { 

NSString *urlAsString = @"https://gdata.youtube.com/feeds/api/videos?q=wankel&v=2&max-results=50&alt=jsonc"; 

UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] init]; 
activityIndicator.color = [UIColor blackColor]; 
activityIndicator.backgroundColor = [UIColor blackColor]; 
[activityIndicator startAnimating]; 
[self.view addSubview:activityIndicator]; 
activityIndicator.center = CGPointMake(self.view.bounds.size.width/2, self.view.bounds.size.height/2); 

NSURL *url = [NSURL URLWithString:urlAsString]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
operation.responseSerializer = [AFJSONResponseSerializer serializer]; 
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

    self.moviesArray = [responseObject valueForKeyPath:@"data.items"]; 
    NSLog(@"%@", moviesArray); 

    self.thumbnailsArray = [responseObject valueForKeyPath:@"data.items.thumbnail"]; 

    self.moviesDetailArray = [responseObject valueForKeyPath:@"data.items"]; 

    [activityIndicator setHidden:YES]; 
    [activityIndicator stopAnimating]; 


    [self.collectionView reloadData]; 

} failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
    UIAlertView *alertView =[[UIAlertView alloc] initWithTitle:@"Error" message:[NSString stringWithFormat:@"%@", error.localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Try again", nil]; 
    [activityIndicator setHidden:YES]; 
    [activityIndicator stopAnimating];  
    [alertView show];   
    }]; 

[operation start]; 
} 
+0

嘗試谷歌搜索「afnetworking停止請求」。已經有幾個堆棧溢出問題。 – hgwhittle

回答

1

您是否嘗試過將運行到AFHTTPRequestOperationManager? 經理有一個operationQueue屬性。您可以從那裏取消操作。

當操作失敗時,停止共享管理器的操作。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
[manager.operationQueue cancelAllOperations]; 

編輯: 如果你正在尋找一個具體的操作,你可以通過在隊列中的操作循環:

for (NSOperation *operation in manager.operationQueue.operations) { 
    // check if this is the right operation, and cancel it. 
} 
0

嘗試把

[operation cancel]; 

在你的不良區

+0

我試過但不起作用 – ArrowStark

相關問題