2014-03-05 60 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

相关问题