2014-03-05 2 views
0

저는 afnetworking 2.0을 사용하여 YouTube 동영상을 표시하고 있습니다. 연결 오류가 발생하면 경고보기를 열고 "확인"을 클릭하면 요청을 중지하고 싶습니다.AF 네트워킹 실패 블록

경고보기가 올바르게 표시되지만 "확인"을 클릭하면 요청이 중지되지 않고 작업 표시기가 나타납니다.

어떻게 요청을 중지 할 수 있습니까?

이 아래의 코드는 다음과 같습니다

-(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

관련 문제